SpringBoot 整合 spring security oauth2 jwt完整示例 附源码

废话不说直接进入主题(假设您已对spring security、oauth2、jwt技术的了解,不懂的自行搜索了解)

依赖版本

  • springboot 2.1.5.RELEASE
  • spring-security-oauth2 2.3.5.RELEASE
  • jjwt 0.9.1

新增JWTokenConfig

@Configuration
public class JWTokenConfig {
   

    @Bean
    public TokenStore jwtTokenStore() {
   
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
   
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey("entfrm"); //对称加密key
        return accessTokenConverter;
    }

    @Bean
    public TokenEnhancer tokenEnhancer() {
   
        return new JWTTokenEnhancer(); // token增强
    }
}

JwtAccessTokenConverter:TokenEnhancer的子类,帮助程序在JWT编码的令牌值和OAuth身份验证信息之间进行转换。
此处定义token 签名的方式,采用对称加密方式。

增加JwtTokenEnhancer类

public class JWTTokenEnhancer implements TokenEnhancer {
   

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
   
        Map<String, Object> info = new HashMap<>()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Spring Boot 整合 Spring Security OAuth2 的代码示例: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.3.RELEASE</version> </dependency> ``` 2. 配置 OAuth2 客户端 在 application.yml 文件中添加以下配置: ``` security: oauth2: client: clientId: your-client-id clientSecret: your-client-secret accessTokenUri: https://your-auth-server.com/oauth/token userAuthorizationUri: https://your-auth-server.com/oauth/authorize clientAuthenticationScheme: form scope: read write ``` 3. 配置 Spring Security 创建一个继承自 WebSecurityConfigurerAdapter 的类,并添加以下配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/oauth/**").permitAll() .anyRequest().authenticated() .and() .formLogin().permitAll() .and() .csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 4. 创建资源服务器 创建一个继承自 ResourceServerConfigurerAdapter 的类,并添加以下配置: ``` @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("resource-id"); } } ``` 5. 创建控制器 创建一个简单的控制器,用于测试 OAuth2 认证: ``` @RestController public class TestController { @GetMapping("/api/test") public String test() { return "Hello, World!"; } } ``` 6. 运行应用程序 现在你可以运行应用程序,并访问 http://localhost:8080/api/test 进行测试。如果你没有提供正确的 OAuth2 认证信息,你将会收到一个 401 Unauthorized 错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值