|
|
|
|
|
|
|
<!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"> package cn.edu.ecnu.stu.bookstore.service.impl;
|
|
|
|
import cn.edu.ecnu.stu.bookstore.component.AppException;
|
|
import cn.edu.ecnu.stu.bookstore.component.Constants;
|
|
import cn.edu.ecnu.stu.bookstore.mapper.UserMapper;
|
|
import cn.edu.ecnu.stu.bookstore.pojo.LoginUser;
|
|
import cn.edu.ecnu.stu.bookstore.pojo.User;
|
|
import cn.edu.ecnu.stu.bookstore.service.UserService;
|
|
import cn.edu.ecnu.stu.bookstore.utils.JwtUtil;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@Service
|
|
<b class="fc"> public class UserServiceImpl implements UserService {</b>
|
|
|
|
@Autowired
|
|
private UserMapper userMapper;
|
|
|
|
@Autowired
|
|
private AuthenticationManager authenticationManager;
|
|
|
|
@Autowired
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
@Autowired
|
|
private RedisTemplate redisTemplate;
|
|
|
|
public void register(User user) {
|
|
<b class="fc"> if(!StringUtils.hasLength(user.getUsername()))</b>
|
|
<b class="nc"> throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
|
|
<b class="fc"> if(userMapper.checkUserByUsername(user.getUsername()) != 0)</b>
|
|
<b class="fc"> throw new AppException(Constants.CLIENT_ERROR, Constants.REGISTER_ERROR_MESSAGE);</b>
|
|
<b class="fc"> String encoded = passwordEncoder.encode(user.getPassword());</b>
|
|
<b class="fc"> user.setPassword(encoded);</b>
|
|
<b class="fc"> userMapper.insert(user);</b>
|
|
}
|
|
|
|
public void unregister(User user) {
|
|
<b class="fc"> User user1 = userMapper.selectOneByName(user.getUsername());</b>
|
|
<b class="fc"> if(user1 == null || !passwordEncoder.matches(user.getPassword(), user1.getPassword())) {</b>
|
|
<b class="nc"> throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
|
|
}
|
|
<b class="fc"> userMapper.deleteByName(user.getUsername());</b>
|
|
}
|
|
|
|
public User selectUserByName(String username) {
|
|
<b class="fc"> return userMapper.selectOneByName(username);</b>
|
|
}
|
|
|
|
public Map<String, Object> login(User user) {
|
|
<b class="fc"> UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());</b>
|
|
<b class="fc"> Authentication authenticate = authenticationManager.authenticate(token);</b>
|
|
<b class="fc"> LoginUser loginUser = (LoginUser)authenticate.getPrincipal();</b>
|
|
<b class="fc"> User user1 = loginUser.getUser();</b>
|
|
<b class="fc"> redisTemplate.opsForValue().set("userId:" + user1.getId(), "1", 1, TimeUnit.DAYS);</b>
|
|
<b class="fc"> Map<String, Object> map = new HashMap<>();</b>
|
|
<b class="fc"> map.put("token", JwtUtil.getToken(user1));</b>
|
|
<b class="fc"> map.put("user", user1);</b>
|
|
<b class="fc"> return map;</b>
|
|
}
|
|
|
|
public void logout(String username) {
|
|
<b class="fc"> User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();</b>
|
|
<b class="fc"> if(!user.getUsername().equals(username)) {</b>
|
|
<b class="nc"> throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
|
|
}
|
|
<b class="fc"> redisTemplate.delete("userId:" + user.getId());</b>
|
|
}
|
|
|
|
public void changePassword(String username, String oldPassword, String newPassword) {
|
|
<b class="fc"> User user = userMapper.selectOneByName(username);</b>
|
|
<b class="fc"> if(user == null || !passwordEncoder.matches(oldPassword, user.getPassword())){</b>
|
|
<b class="nc"> throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
|
|
}
|
|
<b class="fc"> newPassword = passwordEncoder.encode(newPassword);</b>
|
|
<b class="fc"> userMapper.updatePassword(username, newPassword);</b>
|
|
<b class="fc"> redisTemplate.delete("userId:" + user.getId());</b>
|
|
}
|
|
}
|
|
</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>
|