Spring Boot 配置 Security 密码加密

欢迎光临我的博客[http://poetize.cn],前端使用Vue2,聊天室使用Vue3,后台使用Spring Boot

依赖

        <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())
posted @ 2019-08-28 16:28  LittleDonkey  阅读(4136)  评论(0编辑  收藏  举报