Skip to content

Commit abc45a9

Browse files
committed
完善密码加密
1 parent ded0daa commit abc45a9

6 files changed

Lines changed: 39 additions & 22 deletions

File tree

blogserver/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

blogserver/src/main/java/org/sang/config/WebSecurityConfig.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff 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

blogserver/src/main/java/org/sang/controller/LoginRegController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.sang.bean.User;
55
import org.sang.service.UserService;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.PostMapping;
78
import org.springframework.web.bind.annotation.RequestMapping;
89
import 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) {

blogserver/src/main/java/org/sang/service/UserService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import org.sang.bean.Role;
44
import org.sang.bean.User;
5+
import org.sang.config.MyPasswordEncoder;
56
import org.sang.mapper.RolesMapper;
67
import org.sang.mapper.UserMapper;
78
import org.sang.utils.Util;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.security.core.userdetails.UserDetails;
1011
import org.springframework.security.core.userdetails.UserDetailsService;
1112
import org.springframework.security.core.userdetails.UsernameNotFoundException;
13+
import org.springframework.security.crypto.password.PasswordEncoder;
1214
import org.springframework.stereotype.Service;
1315
import org.springframework.transaction.annotation.Transactional;
1416
import 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
//配置用户的角色,默认都是普通用户

blogserver/src/main/resources/application.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spring.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
55
mybatis.config-location=classpath:/mybatis-config.xml
66

77
server.port=8081

0 commit comments

Comments
 (0)