数据库第二次大作业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 lines
6.7 KiB

11 months ago
  1. <!DOCTYPE html>
  2. <html id="htmlId">
  3. <head>
  4. <title>Coverage Report > UserServiceImpl</title>
  5. <style type="text/css">
  6. @import "../../css/coverage.css";
  7. @import "../../css/idea.min.css";
  8. </style>
  9. <script type="text/javascript" src="../../js/highlight.min.js"></script>
  10. <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
  11. </head>
  12. <body>
  13. <div class="content">
  14. <div class="breadCrumbs">
  15. Current scope: <a href="../../index.html">all classes</a>
  16. <span class="separator">|</span>
  17. <a href="../index.html">cn.edu.ecnu.stu.bookstore.service.impl</a>
  18. </div>
  19. <h1>Coverage Summary for Class: UserServiceImpl (cn.edu.ecnu.stu.bookstore.service.impl)</h1>
  20. <table class="coverageStats">
  21. <tr>
  22. <th class="name">Class</th>
  23. <th class="coverageStat
  24. ">
  25. Class, %
  26. </th>
  27. <th class="coverageStat
  28. ">
  29. Method, %
  30. </th>
  31. <th class="coverageStat
  32. ">
  33. Line, %
  34. </th>
  35. </tr>
  36. <tr>
  37. <td class="name">UserServiceImpl</td>
  38. <td class="coverageStat">
  39. <span class="percent">
  40. 100%
  41. </span>
  42. <span class="absValue">
  43. (1/1)
  44. </span>
  45. </td>
  46. <td class="coverageStat">
  47. <span class="percent">
  48. 100%
  49. </span>
  50. <span class="absValue">
  51. (7/7)
  52. </span>
  53. </td>
  54. <td class="coverageStat">
  55. <span class="percent">
  56. 87.5%
  57. </span>
  58. <span class="absValue">
  59. (28/32)
  60. </span>
  61. </td>
  62. </tr>
  63. </table>
  64. <br/>
  65. <br/>
  66. <pre>
  67. <code class="sourceCode" id="sourceCode">&nbsp;package cn.edu.ecnu.stu.bookstore.service.impl;
  68. &nbsp;
  69. &nbsp;import cn.edu.ecnu.stu.bookstore.component.AppException;
  70. &nbsp;import cn.edu.ecnu.stu.bookstore.component.Constants;
  71. &nbsp;import cn.edu.ecnu.stu.bookstore.mapper.UserMapper;
  72. &nbsp;import cn.edu.ecnu.stu.bookstore.pojo.LoginUser;
  73. &nbsp;import cn.edu.ecnu.stu.bookstore.pojo.User;
  74. &nbsp;import cn.edu.ecnu.stu.bookstore.service.UserService;
  75. &nbsp;import cn.edu.ecnu.stu.bookstore.utils.JwtUtil;
  76. &nbsp;import org.springframework.beans.factory.annotation.Autowired;
  77. &nbsp;import org.springframework.data.redis.core.RedisTemplate;
  78. &nbsp;import org.springframework.security.authentication.AuthenticationManager;
  79. &nbsp;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  80. &nbsp;import org.springframework.security.core.Authentication;
  81. &nbsp;import org.springframework.security.core.context.SecurityContextHolder;
  82. &nbsp;import org.springframework.security.crypto.password.PasswordEncoder;
  83. &nbsp;import org.springframework.stereotype.Service;
  84. &nbsp;import org.springframework.util.StringUtils;
  85. &nbsp;
  86. &nbsp;import java.util.HashMap;
  87. &nbsp;import java.util.Map;
  88. &nbsp;import java.util.concurrent.TimeUnit;
  89. &nbsp;
  90. &nbsp;@Service
  91. <b class="fc">&nbsp;public class UserServiceImpl implements UserService {</b>
  92. &nbsp;
  93. &nbsp; @Autowired
  94. &nbsp; private UserMapper userMapper;
  95. &nbsp;
  96. &nbsp; @Autowired
  97. &nbsp; private AuthenticationManager authenticationManager;
  98. &nbsp;
  99. &nbsp; @Autowired
  100. &nbsp; private PasswordEncoder passwordEncoder;
  101. &nbsp;
  102. &nbsp; @Autowired
  103. &nbsp; private RedisTemplate redisTemplate;
  104. &nbsp;
  105. &nbsp; public void register(User user) {
  106. <b class="fc">&nbsp; if(!StringUtils.hasLength(user.getUsername()))</b>
  107. <b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
  108. <b class="fc">&nbsp; if(userMapper.checkUserByUsername(user.getUsername()) != 0)</b>
  109. <b class="fc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.REGISTER_ERROR_MESSAGE);</b>
  110. <b class="fc">&nbsp; String encoded = passwordEncoder.encode(user.getPassword());</b>
  111. <b class="fc">&nbsp; user.setPassword(encoded);</b>
  112. <b class="fc">&nbsp; userMapper.insert(user);</b>
  113. &nbsp; }
  114. &nbsp;
  115. &nbsp; public void unregister(User user) {
  116. <b class="fc">&nbsp; User user1 = userMapper.selectOneByName(user.getUsername());</b>
  117. <b class="fc">&nbsp; if(user1 == null || !passwordEncoder.matches(user.getPassword(), user1.getPassword())) {</b>
  118. <b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
  119. &nbsp; }
  120. <b class="fc">&nbsp; userMapper.deleteByName(user.getUsername());</b>
  121. &nbsp; }
  122. &nbsp;
  123. &nbsp; public User selectUserByName(String username) {
  124. <b class="fc">&nbsp; return userMapper.selectOneByName(username);</b>
  125. &nbsp; }
  126. &nbsp;
  127. &nbsp; public Map&lt;String, Object&gt; login(User user) {
  128. <b class="fc">&nbsp; UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());</b>
  129. <b class="fc">&nbsp; Authentication authenticate = authenticationManager.authenticate(token);</b>
  130. <b class="fc">&nbsp; LoginUser loginUser = (LoginUser)authenticate.getPrincipal();</b>
  131. <b class="fc">&nbsp; User user1 = loginUser.getUser();</b>
  132. <b class="fc">&nbsp; redisTemplate.opsForValue().set(&quot;userId:&quot; + user1.getId(), &quot;1&quot;, 1, TimeUnit.DAYS);</b>
  133. <b class="fc">&nbsp; Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();</b>
  134. <b class="fc">&nbsp; map.put(&quot;token&quot;, JwtUtil.getToken(user1));</b>
  135. <b class="fc">&nbsp; map.put(&quot;user&quot;, user1);</b>
  136. <b class="fc">&nbsp; return map;</b>
  137. &nbsp; }
  138. &nbsp;
  139. &nbsp; public void logout(String username) {
  140. <b class="fc">&nbsp; User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();</b>
  141. <b class="fc">&nbsp; if(!user.getUsername().equals(username)) {</b>
  142. <b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);</b>
  143. &nbsp; }
  144. <b class="fc">&nbsp; redisTemplate.delete(&quot;userId:&quot; + user.getId());</b>
  145. &nbsp; }
  146. &nbsp;
  147. &nbsp; public void changePassword(String username, String oldPassword, String newPassword) {
  148. <b class="fc">&nbsp; User user = userMapper.selectOneByName(username);</b>
  149. <b class="fc">&nbsp; if(user == null || !passwordEncoder.matches(oldPassword, user.getPassword())){</b>
  150. <b class="nc">&nbsp; throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR);</b>
  151. &nbsp; }
  152. <b class="fc">&nbsp; newPassword = passwordEncoder.encode(newPassword);</b>
  153. <b class="fc">&nbsp; userMapper.updatePassword(username, newPassword);</b>
  154. <b class="fc">&nbsp; redisTemplate.delete(&quot;userId:&quot; + user.getId());</b>
  155. &nbsp; }
  156. &nbsp;}
  157. </code>
  158. </pre>
  159. </div>
  160. <script type="text/javascript">
  161. (function() {
  162. var msie = false, msie9 = false;
  163. /*@cc_on
  164. msie = true;
  165. @if (@_jscript_version >= 9)
  166. msie9 = true;
  167. @end
  168. @*/
  169. if (!msie || msie && msie9) {
  170. hljs.highlightAll()
  171. hljs.initLineNumbersOnLoad();
  172. }
  173. })();
  174. </script>
  175. <div class="footer">
  176. <div style="float:right;">generated on 2023-12-12 18:32</div>
  177. </div>
  178. </body>
  179. </html>