一:什么是Spring Security
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。它的核心是一组过滤器链,不同的功能经由不同的过滤器。
二:认证授权功能实现
1,准备一个springCloud的工程,引入oAuth2的依赖
<!-- oAuth2 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
2,创建基于内存
的认证/授权
配置类
@EnableWebSecurity
@Configuration
public class webSecurityConfig extends WebSecurityConfigurerAdapter {
//密码编译比对方式
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
//访问路径配置
@Override
protected void configure(HttpSecurity http) throws java.lang.Exception {
//.hasAuthority : 基于资源授权 .hasRole:基于角色授权
http.authorizeRequests()
.antMatchers("/auth/hello").hasRole("GUEST") //给访问的地址加上角色限制,只有指定的角色才能访问
.antMatchers("/auth/home").hasRole("USER")
.antMatchers("/auth/admin&