0
点赞
收藏
分享

微信扫一扫

Spring Cloud入门-Oauth2授权之JWT集成(Hoxton版本)

爪哇驿站 2022-04-21 阅读 98

可以在该网站上获得解析结果:[https://jwt.io/](()

在这里插入图片描述

[](()创建oauth2-jwt-server模块


该模块只是对oauth2-server模块的扩展,直接复制过来扩展下下即可。

[](()oauth2中存储令牌的方式


[](()使用Redis存储令牌

在pom.xml中添加Redis相关依赖:

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-oauth2

org.springframework.cloud

spring-cloud-starter-security

在application.yml中添加redis相关配置:

server:

port: 9401

spring:

application:

name: oauth2-jwt-server

redis:

redis相关配置

host: 10.172.0.201

database: 0

添加在Redis中存储令牌的配置:

@Configuration

public class RedisTokenStoreConfig {

@Autowired

private RedisConnectionFactory redisConnectionFactory;

@Bean

public TokenStore redisTokenStore() {

return new RedisTokenStore(redisConnectionFactory);

}

}

在授权服务器配置中指定令牌的存储策略为Redis:

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired

private PasswordEncoder passwordEncoder;

@Autowired

private AuthenticationManager authenticationManager;

@Autowired

private UserService userService;

@Autowired

@Qualifier(“redisTokenStore”)

private TokenStore tokenStore;

/**

  • 使用密码模式需要配置

*/

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

endpoin 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》开源 ts.authenticationManager(authenticationManager)

.userDetailsService(userService)

//配置令牌存储策略

.tokenStore(tokenStore);

}

//省略代码…

}

运行项目后使用密码模式来获取令牌,访问如下地址:[http://localhost:9401/oauth/token](()

在这里插入图片描述

进行获取令牌操作,可以发现令牌已经被存储到Redis中。

在这里插入图片描述

[](()使用JWT存储令牌

添加使用JWT存储令牌的配置:

@Configuration

public class JwtTokenStoreConfig {

@Bean

@Primary

public TokenStore jwtTokenStore() {

return new JwtTokenStore(jwtAccessTokenConverter());

}

@Bean

public JwtTokenEnhancer jwtTokenEnhancer() {

return new JwtTokenEnhancer();

}

@Bean

public JwtAccessTokenConverter jwtAccessTokenConverter() {

JwtAccessTokenConverter jwtAccessTok Java开源项目【ali1024.coding.net/public/P7/Java/git】 enConverter = new JwtAccessTokenConverter();

// 配置jwt使用的密钥

jwtAccessTokenConverter.setSigningKey(“test_key”);

return jwtAccessTokenConverter;

}

}

在授权服务器配置中指定令牌的存储策略为JWT:

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired

private PasswordEncoder passwordEncoder;

@Autowired

private AuthenticationManager authenticationManager;

@Autowired

private UserService userService;

@Autowired

@Qualifier(“jwtTokenStore”)

private TokenStore tokenStore;

@Autowired

private JwtAccessTokenConverter jwtAccessTokenConverter;

@Autowired

private JwtTokenEnhancer jwtTokenEnhancer;

/**

  • 使用密码模式需要配置

*/

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

endpoints.authenticationManager(authenticationManager)

.userDetailsService(userService)

//配置令牌存储策略

.tokenStore(tokenStore)

.accessTokenConverter(jwtAccessTokenConverter);

}

//省略代码…

}

运行项目后使用密码模式来获取令牌,访问如下地址:[http://localhost:9401/oauth/token](()

在这里插入图片描述

发现获取到的令牌已经变成了JWT令牌,将access_token拿到https://jwt.io/ 网站上去解析下可以获得其中内容。

{

“exp”: 1577678092,

“user_name”: “jourwon”,

“authorities”: [

“admin”

],

“jti”: “87db4bdf-2936-420d-87b9-fb580e255a4d”,

“client_id”: “admin”,

“scope”: [

“all”

]

}

[](()扩展JWT中存储的内容


继承TokenEnhancer实现一个JWT内容增强器:

public class JwtTokenEnhancer implements TokenEnhancer {

@Override

public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {

Map<String, Object> info = new HashMap<>();

info.put(“enhance”, “enhance info”);

((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(info);

return oAuth2AccessToken;

}

}

创建一个JwtTokenEnhancer实例:

@Configuration

public class JwtTokenStoreConfig {

//省略代码…

@Bean

public JwtTokenEnhancer jwtTokenEnhancer() {

return new JwtTokenEnhancer();

}

}

在授权服务器配置中配置JWT的内容增强器:

总结

这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家

在这里插入图片描述

在这里插入图片描述

n).setAdditionalInformation(info);

return oAuth2AccessToken;

}

}

创建一个JwtTokenEnhancer实例:

@Configuration

public class JwtTokenStoreConfig {

//省略代码…

@Bean

public JwtTokenEnhancer jwtTokenEnhancer() {

return new JwtTokenEnhancer();

}

}

在授权服务器配置中配置JWT的内容增强器:

总结

这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家

[外链图片转存中…(img-0NMu2iuN-1650439404804)]

[外链图片转存中…(img-qtE8uNqK-1650439404804)]

举报

相关推荐

0 条评论