当前位置:网站首页>Oauth2.0 资源服务器搭建
Oauth2.0 资源服务器搭建
2022-08-02 14:14:00 【zhangyu丶】
配置 WebSecurityConfig 开启 Spring 方法级的安全保护
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());
}
//必须注入,并保证对称密钥和认证服务器的一样
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(OauthConstant.OAUTH_SIGNING_KEY); //对称秘钥,资源服务器使用该秘钥来验证
return converter;
}
}
继承 ResourceServerConfigurerAdapter 来实现资源服务器的核心配置️️️️️️️️️️️️
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 补充
边栏推荐
猜你喜欢
极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件
tcp transparent proxy (IP_TRANSPARENT)
基类和派生类的关系【继承】/多态和虚函数/【继承和多态】抽象类和简单工厂
implement tcp copa on ns3
Based on the least squares linear regression equation coefficient estimation
Unity-Post Processing
audio console无法连接到RPC服务
C#实现简单的计算器
光波导的入射耦合和出射耦合区域
【线程安全】用户级,内核级,组合级线程|线程同步的处理(条件变量)|strtok_r(可冲入函数)
随机推荐
golang的内存相关内容
2021-03-12
shader入门精要2
如何编辑VirtualLab Fusion结果的格式
Run ns3 with multiple processes
动态数组-vector
px和em和rem的区别
2. Log out, log in state examination, verification code
Unity-存档与读档
分布式一致性协议-Raft
光波导k域布局可视化(“神奇的圆环”)
光波导应用中的真实光栅效应
Doubled and sparse tables
开源一个golang写的游戏服务器框架
Unity插件-NGUI
数学工具-desmos 图形曲线
光导布局设计工具
Qt | 读取文件内容并删除文件 QFile
Happy, 9/28 scene collection
关于分布式的一些知识点