我正在尝试使用spring security 5检索一个不透明令牌的值,这样我就可以将其传递给另一个rest应用程序以获取其他资源。下面是我的安全配置:
@EnableWebSecurity公共类OAuth2ResourceServerSecurityConfiguration扩展了WebSecurityConfigurerAdapter {
@Value("${spring.security.oauth2.resourceserver.opaque.introspection-uri}") String introspectionUri;
@Value("${spring.security.oauth2.resourceserver.opaque.introspection-client-id}") String clientId;
@Value("${spring.security.oauth2.resourceserver.opaque.introspection-client-secret}") String clientSecret;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests((authorizeRequests) ->
authorizeRequests
.antMatchers(HttpMethod.GET, "/message/**").hasAuthority("SCOPE_message:read")
.antMatchers(HttpMethod.POST, "/message/**").hasAuthority("SCOPE_message:write")
.anyRequest().authenticated()
)
.oauth2ResourceServer((oauth2ResourceServer) ->
oauth2ResourceServer
.opaqueToken((opaqueToken) ->
opaqueToken
.introspectionUri(this.introspectionUri)
.introspectionClientCredentials(this.clientId, this.clientSecret)
)
);
}控制器看起来像这样:
@GetMapping("/")
public String index(@AuthenticationPrincipal(expression="subject") String subject) {
return String.format("Hello, %s!", subject);
}我想从控制器内部访问令牌,以便将其转发到下一个服务。
我该怎么做呢?
发布于 2021-03-24 04:26:26
您可以在控制器方法中使用以下参数访问Spring MVC中的HTTP header Authorization。
@RequestHeader("Authorization") String header)请注意,它们可以是不记名标记,如果它们确实是不记名标记,则需要在"Bearer " (注意空格)之后使用一个子字符串。
https://stackoverflow.com/questions/66770708
复制相似问题