我用我的 Spring Boot 应用程序配置了一个 Hydra 实例。我刚刚使用注释 @EnableResourceServer
将我的应用程序配置为资源服务器.因此,当我在请求中使用 Bearer 授权 header 时,Spring 使用我在属性中指定的值:
security.oauth2.resource.user-info-uri=...
验证 token 是否有效。不幸的是,我没有找到 Hydra OAuth 2.0 的这个 URL (http://docs.hydra13.apiary.io//https://github.com/ory/hydra)
最佳答案
首先:在 Ory Hydra 中配置您的资源服务器(您必须使用 client_credentials 和范围 'hydra.introspect' 将其添加到 Ory Hydra 以便能够请求 token 有效性):
$> hydra clients create --skip-tls-verify \
--id my-rest-api \
--secret mypwd \
--grant-types client_credentials \
--response-types token \
--allowed-scopes hydra.introspect
第二:添加一个策略,让您的资源服务器请求 token 有效性。
$> hydra policies create --skip-tls-verify \
--actions introspect \
--description "Policy to introspect tokens from my api" \
--allow \
--id accesstoken_introsp-policy \
--resources "rn:hydra:oauth2:tokens" \
--subjects my-rest-api
第三:在 build.gradle 中添加 oauth2 依赖项(如果是 maven,则添加 pom.xml):
compile 'org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE'
第四:配置 application.yml 以使用 Ory Hydra 自省(introspection)端点获取 token 信息。
security:
user:
password: none
oauth2:
resource:
token-info-uri: https://yourserver.com/oauth2/introspect
client:
client-id: my-rest-api
client-secret: mypwd
scope: [ "hydra.introspect" ]
第五:创建一个类来配置受访问 token 保护的url
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
private RemoteTokenServices tokenServices;
@Value("${security.oauth2.client.client-id}")
private String clientId;
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/api/v1/**").access("#oauth2.hasScope('my.desired.scope')")
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId(clientId);
tokenServices.setAccessTokenConverter(new OryHydraAccessTokenConverter());
resources.tokenServices(tokenServices);
}
}
class OryHydraAccessTokenConverter extends DefaultAccessTokenConverter {
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
OAuth2Authentication oAuth2Authentication = super.extractAuthentication(map);
oAuth2Authentication.setDetails(map.get("ext"));
return oAuth2Authentication;
}
}
我需要一个自定义 AccessTokenConverter,因为我的同意应用程序向 token 添加了几个属性,我们需要映射所有这些属性。使用 Ory,我的属性位于“ext”属性下。这是一个示例访问 token :
{
"active": true,
"scope": "my.desired.scope",
"client_id": "my-mobile-app",
"sub": "123121e",
"exp": 1520948372,
"iat": 1520944772,
"iss": "https://yourserver.com",
"ext": {
"custom_prop1": 12321,
"custom_prop2": "Name Surname",
"custom_prop3": false
}
}
最后一步:现在在您的 Controller 中,您可以 Autowiring 为参数 Oauth2Authentication 对象。
@GetMapping("/api/v1/data")
public MyBean findDataById(OAuth2Authentication auth,
@RequestParam("id") String id) {
OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) auth.getDetails();
Map<String, Object> ext = (Map<String, Object>) oAuth2AuthenticationDetails.getDecodedDetails();
return MyBean.builder().name("Name:"+ext.get("custom_prop1")).build();
}
关于java - 使用 Hydra OAuth 2.0 配置 Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714067/