当前位置:网站首页>Oauth2.0 resource server construction
Oauth2.0 resource server construction
2022-08-02 16:03:00 【zhangyu】
配置 WebSecurityConfig 开启 Spring Method-level security protection
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated();
}
}
配置 TokenConfig 来定义 Token 的校验方式
import cn.mowen.common.constant.OauthConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
@Configuration
public class TokenConfig {
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
//必须注入,And ensure that the symmetric key and authentication server
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(OauthConstant.OAUTH_SIGNING_KEY); //对称秘钥,资源服务器使用该秘钥来验证
return converter;
}
}
继承 ResourceServerConfigurerAdapter To realize the resource server core configuration️️️️️️️️️️️️
import cn.mowen.common.constant.OauthConstant;
import cn.mowen.common.constant.CommonWhiteConstant;
import cn.mowen.common.exception.oauth.CustomAuthenticationEntryPoint;
import cn.mowen.common.exception.oauth.CustomAccessDeniedHandler;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
@Configuration
@EnableResourceServer
@AllArgsConstructor
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
private final TokenStore jwtTokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId(OauthConstant.OAUTH_RESOURCE_ID)
.tokenStore(jwtTokenStore)
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.accessDeniedHandler(new CustomAccessDeniedHandler())
.stateless(true)
;
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
//放行 url 在此配置
.antMatchers(CommonWhiteConstant.white).permitAll()
.antMatchers(white).permitAll()
.anyRequest().authenticated()
;
}
// 白名单
private static final String[] white = {
"/test/**"
};
}
Oauth2.0 系列文章
以下是同步到语雀的、可读性好一点,CSDN 继续看的点专栏就好.
Oauth2.0 核心篇
Oauth2.0 安全性(以微信授权登陆为例)
Oauth2.0 认证服务器搭建
Oauth2.0 添加验证码登陆方式
Oauth2.0 资源服务器搭建
Oauth2.0 自定义响应值以及异常处理
Oauth2.0 补充
边栏推荐
- Test case exercises
- CDH(computational Diffie-Hellman)问题以及与离散对数、DDH问题的区别
- 分布式一致性协议-Raft
- couldn't find 'libflutter.so' --flutter
- golang-reflect-method-callback
- 剑指offer:合并两个排序的链表
- 学习笔记(01):activiti6.0从入门到精通-工作流的介绍以及插件的安装
- The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories
- UnityAPI-Ray-Physics
- 类模板/赋值运算和加等运算
猜你喜欢
随机推荐
使用三个线程,按顺序打印X,Y,Z,连续打印10次
分布式一致性协议-Raft
CDH (computational Diffie-Hellman) problem and its differences with discrete logarithm and DDH problems
深入理解负载均衡
类模板/赋值运算和加等运算
Qt | 读取文件内容并删除文件 QFile
golang gc垃圾回收
嵌入式学习硬件篇------初识ARM
implement tcp copa on ns3
unity-shader(中级)
关于推荐系统的随想
unity Domain Reload & scene Reload 静态变量重置
【进程间通信】消息队列
Unity-Post Processing
移动拷贝构造函数
剑指offer:在O(1)时间删除链表结点
泰伯效应的建模
使用1D-1D EPE的光波导布局设计工具
2342. 数位和相等数对的最大和 哈希优化
Qt | 播放音频文件 QMediaplayer