Spring Security
Spring Security 是一个spring的安全框架
oAuth2
是一种授权协议,他的主要作用是为了提供认证和授权的标准
Access Token
客户端访问资源服务器的令牌,代表用户的授权
Refresh Token
刷新令牌,是用来实现安全令牌的时效机制,使通过安全令牌访问的资源更加安全
配置认证服务器
在云服务中一般有认证服务器和资源服务器,通常认证服务器用来获取用户的登录和授权信息,而资源服务器则用来校验具体的权限信息
认证服务器注解
@Configuration
@EnableAuthorizationServer
认证服务器继承类
认证服务器继承AuthorizationServerConfigurerAdapter
认证服务器通过客户端的id,密钥,客户端授权类型取得客户端授权范围
资源服务代码示例
1 | import org.springframework.beans.factory.annotation.Autowired; |
服务器安全配置
创建一个类继承 WebSecurityConfigurerAdapter 并添加相关注解(@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
)
服务器安全配置代码示例
1 | /** |
配置资源服务器
资源服务器则用来校验具体的权限信息
资源服务器注解
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
资源服务器继承类
资源服务器继承ResourceServerConfigurerAdapter
资源服务器通声明自身资源ID和需要的角色权限
资源服务器代码示例
1 |
|
yml文件配置
1 | security: |