数据库第二次大作业boostore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

193 regels
6.7 KiB

<!DOCTYPE html>
<html id="htmlId">
<head>
<title>Coverage Report > UserServiceImpl</title>
<style type="text/css">
@import "../../css/coverage.css";
@import "../../css/idea.min.css";
</style>
<script type="text/javascript" src="../../js/highlight.min.js"></script>
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>
<body>
<div class="content">
<div class="breadCrumbs">
Current scope: <a href="../../index.html">all classes</a>
<span class="separator">|</span>
<a href="../index.html">cn.edu.ecnu.stu.bookstore.service.impl</a>
</div>
<h1>Coverage Summary for Class: UserServiceImpl (cn.edu.ecnu.stu.bookstore.service.impl)</h1>
<table class="coverageStats">
<tr>
<th class="name">Class</th>
<th class="coverageStat
">
Class, %
</th>
<th class="coverageStat
">
Method, %
</th>
<th class="coverageStat
">
Line, %
</th>
</tr>
<tr>
<td class="name">UserServiceImpl</td>
<td class="coverageStat">
<span class="percent">
100%
</span>
<span class="absValue">
(1/1)
</span>
</td>
<td class="coverageStat">
<span class="percent">
100%
</span>
<span class="absValue">
(7/7)
</span>
</td>
<td class="coverageStat">
<span class="percent">
87.5%
</span>
<span class="absValue">
(28/32)
</span>
</td>
</tr>
</table>
<br/>
<br/>
<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package cn.edu.ecnu.stu.bookstore.service.impl;
&nbsp;
&nbsp;import cn.edu.ecnu.stu.bookstore.component.AppException;
&nbsp;import cn.edu.ecnu.stu.bookstore.component.Constants;
&nbsp;import cn.edu.ecnu.stu.bookstore.mapper.UserMapper;
&nbsp;import cn.edu.ecnu.stu.bookstore.pojo.LoginUser;
&nbsp;import cn.edu.ecnu.stu.bookstore.pojo.User;
&nbsp;import cn.edu.ecnu.stu.bookstore.service.UserService;
&nbsp;import cn.edu.ecnu.stu.bookstore.utils.JwtUtil;
&nbsp;import org.springframework.beans.factory.annotation.Autowired;
&nbsp;import org.springframework.data.redis.core.RedisTemplate;
&nbsp;import org.springframework.security.authentication.AuthenticationManager;
&nbsp;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
&nbsp;import org.springframework.security.core.Authentication;
&nbsp;import org.springframework.security.core.context.SecurityContextHolder;
&nbsp;import org.springframework.security.crypto.password.PasswordEncoder;
&nbsp;import org.springframework.stereotype.Service;
&nbsp;import org.springframework.util.StringUtils;
&nbsp;
&nbsp;import java.util.HashMap;
&nbsp;import java.util.Map;
&nbsp;import java.util.concurrent.TimeUnit;
&nbsp;
&nbsp;@Service
<b class="fc">&nbsp;public class UserServiceImpl implements UserService {</b>
&nbsp;
&nbsp; @Autowired
&nbsp; private UserMapper userMapper;
&nbsp;
&nbsp; @Autowired
&nbsp; private AuthenticationManager authenticationManager;
&nbsp;
&nbsp; @Autowired
&nbsp; private PasswordEncoder passwordEncoder;
&nbsp;
&nbsp; @Autowired
&nbsp; private RedisTemplate redisTemplate;
&nbsp;
&nbsp; public void register(User user) {
<b class="fc">&nbsp; if(!StringUtils.hasLength(user.getUsername()))</b>
<b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
<b class="fc">&nbsp; if(userMapper.checkUserByUsername(user.getUsername()) != 0)</b>
<b class="fc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.REGISTER_ERROR_MESSAGE);</b>
<b class="fc">&nbsp; String encoded = passwordEncoder.encode(user.getPassword());</b>
<b class="fc">&nbsp; user.setPassword(encoded);</b>
<b class="fc">&nbsp; userMapper.insert(user);</b>
&nbsp; }
&nbsp;
&nbsp; public void unregister(User user) {
<b class="fc">&nbsp; User user1 = userMapper.selectOneByName(user.getUsername());</b>
<b class="fc">&nbsp; if(user1 == null || !passwordEncoder.matches(user.getPassword(), user1.getPassword())) {</b>
<b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
&nbsp; }
<b class="fc">&nbsp; userMapper.deleteByName(user.getUsername());</b>
&nbsp; }
&nbsp;
&nbsp; public User selectUserByName(String username) {
<b class="fc">&nbsp; return userMapper.selectOneByName(username);</b>
&nbsp; }
&nbsp;
&nbsp; public Map&lt;String, Object&gt; login(User user) {
<b class="fc">&nbsp; UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());</b>
<b class="fc">&nbsp; Authentication authenticate = authenticationManager.authenticate(token);</b>
<b class="fc">&nbsp; LoginUser loginUser = (LoginUser)authenticate.getPrincipal();</b>
<b class="fc">&nbsp; User user1 = loginUser.getUser();</b>
<b class="fc">&nbsp; redisTemplate.opsForValue().set(&quot;userId:&quot; + user1.getId(), &quot;1&quot;, 1, TimeUnit.DAYS);</b>
<b class="fc">&nbsp; Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();</b>
<b class="fc">&nbsp; map.put(&quot;token&quot;, JwtUtil.getToken(user1));</b>
<b class="fc">&nbsp; map.put(&quot;user&quot;, user1);</b>
<b class="fc">&nbsp; return map;</b>
&nbsp; }
&nbsp;
&nbsp; public void logout(String username) {
<b class="fc">&nbsp; User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();</b>
<b class="fc">&nbsp; if(!user.getUsername().equals(username)) {</b>
<b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
&nbsp; }
<b class="fc">&nbsp; redisTemplate.delete(&quot;userId:&quot; + user.getId());</b>
&nbsp; }
&nbsp;
&nbsp; public void changePassword(String username, String oldPassword, String newPassword) {
<b class="fc">&nbsp; User user = userMapper.selectOneByName(username);</b>
<b class="fc">&nbsp; if(user == null || !passwordEncoder.matches(oldPassword, user.getPassword())){</b>
<b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
&nbsp; }
<b class="fc">&nbsp; newPassword = passwordEncoder.encode(newPassword);</b>
<b class="fc">&nbsp; userMapper.updatePassword(username, newPassword);</b>
<b class="fc">&nbsp; redisTemplate.delete(&quot;userId:&quot; + user.getId());</b>
&nbsp; }
&nbsp;}
</code>
</pre>
</div>
<script type="text/javascript">
(function() {
var msie = false, msie9 = false;
/*@cc_on
msie = true;
@if (@_jscript_version >= 9)
msie9 = true;
@end
@*/
if (!msie || msie && msie9) {
hljs.highlightAll()
hljs.initLineNumbersOnLoad();
}
})();
</script>
<div class="footer">
<div style="float:right;">generated on 2023-12-12 18:32</div>
</div>
</body>
</html>