之前我们做了用户模块,但是忘记写了一些加密和鉴所以我们需要继续优化用户模块
BCrypt密码加密:

步骤一 需要在pom 加入 spring-security的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
步骤二 添加配置类

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//主路径不被拦截
.antMatchers("/").permitAll()
//拦截其他路径
.anyRequest().authenticated()
.and()
//注销请求不被拦截
.logout().permitAll()
.and()
//支持表单登录
.formLogin();
//关闭默认的csrf认证
http.csrf().disable();
}
}
并在启动配置类里面加入 BCryptPasswordEncoder

我们在service 层注入 并在 添加用户的service 里面使用到加密


我们在登录的时候密码加密了 但是登录的时候当然也需要使用工具去校验
首先在dao层加入 :
Admin findByLoginname(String loginname);
接下来在service层加入:

接下来在controller 层加入:

网友评论