File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414 <parent >
1515 <groupId >org.springframework.boot</groupId >
1616 <artifactId >spring-boot-starter-parent</artifactId >
17- <version >1.5.9 .RELEASE</version >
17+ <version >2.2.7 .RELEASE</version >
1818 <relativePath /> <!-- lookup parent from repository -->
1919 </parent >
2020
Original file line number Diff line number Diff line change 1+ package org .sang .config ;
2+
3+ import org .springframework .security .crypto .password .PasswordEncoder ;
4+ import org .springframework .stereotype .Component ;
5+ import org .springframework .util .DigestUtils ;
6+
7+ /**
8+ * @作者 江南一点雨
9+ * @微信公众号 江南一点雨
10+ * @网站 http://www.itboyhub.com
11+ * @国际站 http://www.javaboy.org
12+ * @微信 a_java_boy
13+ * @GitHub https://github.com/lenve
14+ * @Gitee https://gitee.com/lenve
15+ */
16+ @ Component
17+ public class MyPasswordEncoder implements PasswordEncoder {
18+ @ Override
19+ public String encode (CharSequence rawPassword ) {
20+ return DigestUtils .md5DigestAsHex (rawPassword .toString ().getBytes ());
21+ }
22+
23+ @ Override
24+ public boolean matches (CharSequence rawPassword , String encodedPassword ) {
25+ return encodedPassword .equals (DigestUtils .md5DigestAsHex (rawPassword .toString ().getBytes ()));
26+ }
27+ }
Original file line number Diff line number Diff line change @@ -32,22 +32,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
3232
3333 @ Override
3434 protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
35- auth .userDetailsService (userService ).passwordEncoder (new PasswordEncoder () {
36- @ Override
37- public String encode (CharSequence charSequence ) {
38- return DigestUtils .md5DigestAsHex (charSequence .toString ().getBytes ());
39- }
40-
41- /**
42- * @param charSequence 明文
43- * @param s 密文
44- * @return
45- */
46- @ Override
47- public boolean matches (CharSequence charSequence , String s ) {
48- return s .equals (DigestUtils .md5DigestAsHex (charSequence .toString ().getBytes ()));
49- }
50- });
35+ auth .userDetailsService (userService );
5136 }
5237
5338 @ Override
Original file line number Diff line number Diff line change 44import org .sang .bean .User ;
55import org .sang .service .UserService ;
66import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .web .bind .annotation .PostMapping ;
78import org .springframework .web .bind .annotation .RequestMapping ;
89import org .springframework .web .bind .annotation .RestController ;
910
@@ -38,7 +39,7 @@ public RespBean loginPage() {
3839 return new RespBean ("error" , "尚未登录,请登录!" );
3940 }
4041
41- @ RequestMapping ("/reg" )
42+ @ PostMapping ("/reg" )
4243 public RespBean reg (User user ) {
4344 int result = userService .reg (user );
4445 if (result == 0 ) {
Original file line number Diff line number Diff line change 22
33import org .sang .bean .Role ;
44import org .sang .bean .User ;
5+ import org .sang .config .MyPasswordEncoder ;
56import org .sang .mapper .RolesMapper ;
67import org .sang .mapper .UserMapper ;
78import org .sang .utils .Util ;
89import org .springframework .beans .factory .annotation .Autowired ;
910import org .springframework .security .core .userdetails .UserDetails ;
1011import org .springframework .security .core .userdetails .UserDetailsService ;
1112import org .springframework .security .core .userdetails .UsernameNotFoundException ;
13+ import org .springframework .security .crypto .password .PasswordEncoder ;
1214import org .springframework .stereotype .Service ;
1315import org .springframework .transaction .annotation .Transactional ;
1416import org .springframework .util .DigestUtils ;
@@ -25,6 +27,8 @@ public class UserService implements UserDetailsService {
2527 UserMapper userMapper ;
2628 @ Autowired
2729 RolesMapper rolesMapper ;
30+ @ Autowired
31+ PasswordEncoder passwordEncoder ;
2832
2933 @ Override
3034 public UserDetails loadUserByUsername (String s ) throws UsernameNotFoundException {
@@ -51,7 +55,7 @@ public int reg(User user) {
5155 return 1 ;
5256 }
5357 //插入用户,插入之前先对密码进行加密
54- user .setPassword (DigestUtils . md5DigestAsHex (user .getPassword (). getBytes ()));
58+ user .setPassword (passwordEncoder . encode (user .getPassword ()));
5559 user .setEnabled (true );//用户可用
5660 long result = userMapper .reg (user );
5761 //配置用户的角色,默认都是普通用户
Original file line number Diff line number Diff line change 11spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
2- spring.datasource.url =jdbc:mysql:///vueblog ?useUnicode =true&characterEncoding =UTF-8
3- spring.datasource.username =username
4- spring.datasource.password =password
2+ spring.datasource.url =jdbc:mysql:///vueblog2 ?useUnicode =true&characterEncoding =UTF-8& serverTimezone =Asia/Shanghai
3+ spring.datasource.username =root
4+ spring.datasource.password =123
55mybatis.config-location =classpath:/mybatis-config.xml
66
77server.port =8081
You can’t perform that action at this time.
0 commit comments