Spring Boot 配置 Security 密码加密

依赖

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

注入bean

@SpringBootApplication
public class UserApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

    @Bean
    public BCryptPasswordEncoder encoding(){
        return new BCryptPasswordEncoder();
    }
}

安全配置类

authenticated()要求认证后才能访问。
如果用户没有认证的话,Spring SecurityFilter将会捕获该请求,并将用户重定向到应用的登录页面。

/**
 * 安全配置类
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //authorizeRequests:声明配置是权限配置
        //antMatchers:路径
        //permitAll:任何权限都可以访问,不需要身份认证
        //anyRequest:任何请求
        //authenticated:认证后才能访问
        //and().csrf().disable():固定写法,表示csrf拦截失效

        http
            .authorizeRequests()
            .antMatchers("/**").permitAll()
            .anyRequest().authenticated()
            .and().csrf().disable();
    }
}


access(String) 如果给定的SpEL表达式计算结果为true,就允许访问

anonymous() 允许匿名用户访问

authenticated() 允许认证的用户进行访问

denyAll() 无条件拒绝所有访问

fullyAuthenticated() 如果用户是完整认证的话(不是通过Remember-me功能认证的),就允许访问

hasAuthority(String) 如果用户具备给定权限的话就允许访问

hasAnyAuthority(String…) 如果用户具备给定权限中的某一个的话,就允许访问

hasRole(String) 如果用户具备给定角色(用户组)的话,就允许访问

hasAnyRole(String…) 如果用户具有给定角色(用户组)中的一个的话,允许访问

hasIpAddress(String) 如果请求来自给定ip地址的话,就允许访问

not() 对其他访问结果求反

permitAll() 所有权限无条件允许访问

rememberMe() 如果用户是通过Remember-me功能认证的,就允许访问

密码加密与解密

    @Autowired
    private BCryptPasswordEncoder encoding;

    public void add(Admin admin) {
        //加密
        admin.setPassword(encoding.encode(admin.getPassword()));
        adminDao.save(admin);
    }


    encoding.matches(admin.getPassword(),sqlAdmain.getPassword())

转载于:https://www.cnblogs.com/loveer/p/11424821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值