用户模块密码加密

2019-03-14  本文已影响0人  神豪VS勇士赢

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

BCrypt密码加密:


image.png

步骤一 需要在pom 加入 spring-security的依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

步骤二 添加配置类

image.png

@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


image.png

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

image.png
image.png

我们在登录的时候密码加密了 但是登录的时候当然也需要使用工具去校验

首先在dao层加入 :
Admin findByLoginname(String loginname);
接下来在service层加入:


image.png

接下来在controller 层加入:


image.png
上一篇 下一篇

猜你喜欢

热点阅读