【发布时间】:2018-05-04 11:50:56
【问题描述】:
我已将 spring oauth2 添加到我的宁静服务中。大多数服务都由我自己的门户使用,因此获取令牌然后调用 api 就可以了。但是,我已经公开了一些必须在没有此令牌概念的情况下调用的 Web 服务。这些消费者有用户名和密码。
最好的例子是 Swagger 实现。应该通过摘要而不是 oauth 令牌来验证打开 swagger 页面的位置。
我做了以下代码更改,但它不起作用。 我相信,在这种情况下,我不需要从资源服务器调用 oauth 服务器。所以只需在资源服务器中制作以下代码。但是看到问题,例如在身份验证页面接受我的凭据后,它再次重新路由/重定向到同一身份验证页面。
请帮忙。
@Configuration
@EnableResourceServer
public class ResourceServerImpl extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.anonymous().disable().requestMatchers().antMatchers("/**").and().authorizeRequests()
.antMatchers(HttpMethod.POST, "/url1/path1/path2").hasAnyAuthority( "FUNCTION_FN1")
.antMatchers(HttpMethod.GET, "/url2/path1/path2").hasAnyAuthority( "FUNCTION_FN2")
.antMatchers("/swagger-ui.html").hasRole("USER")
.anyRequest().authenticated()
.and().formLogin().and().httpBasic()
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
@Configuration
@EnableWebSecurity
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}
更新 我认为这不可能提出一些通过 DIGEST 进行身份验证的请求,而有些是通过 OAUTH 进行身份验证的。所以目前我制作了 swagger url 也使用 oauth 令牌进行身份验证,而不是摘要支持。
【问题讨论】:
-
你没有提供任何关于配置的代码,所以你为什么要问我必须做哪些代码更改?
-
@AtaurRahmanMunna 当然,我会提供,但这可行吗...?
-
但是我暴露了更多的网络服务——所以对于其他消费者和 oauth2 消费者来说,端点是不同的,对吧?
-
是的,阿陶尔。 URL 不同,但服务是单一的,但我希望在两种情况下都遵循相同的访问规则(授权)。
-
您需要为 httpBasic() 提供单独的配置。参考 - stackoverflow.com/questions/27774742/…
标签: spring spring-security spring-oauth2