Spring OAuth @EnableResourceServer 阻止来自 OAuth 服务器的登录页面
Posted
技术标签:
【中文标题】Spring OAuth @EnableResourceServer 阻止来自 OAuth 服务器的登录页面【英文标题】:Spring OAuth @EnableResourceServer preventing login page from OAuth server 【发布时间】:2015-06-16 10:37:39 【问题描述】:localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com 的浏览器响应是 302 Found,但是 localhost:9999/uaa/login 的响应是 401 Unauthorized。
我可以在添加@EnableResourceServer 之前获取登录令牌。我正在使用 Spring boot 并扩展 WebSecurityConfigurerAdapter 以将身份验证管理器与数据源一起使用。当我尝试添加 ResourceServerConfigurerAdapter 时,它不会构建。允许登录页面的最简单方法是什么?
@SpringBootApplication
@RestController
@EnableResourceServer
public class OAuthSvcApplication extends WebMvcConfigurerAdapter
private static final Logger log = LoggerFactory.getLogger(OAuthSvcApplication.class);
@RequestMapping("/user")
public Principal user(Principal user)
return user;
public static void main(String[] args)
SpringApplication.run(OAuthSvcApplication.class, args);
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
@Autowired
public void configureAuth(AuthenticationManagerBuilder auth,DataSource dataSource, Environment env)
throws Exception
auth.jdbcAuthentication().dataSource(dataSource);
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private DataSource dataSource;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception
endpoints.authenticationManager(authenticationManager);
@Override
public void configure(AuthorizationServerSecurityConfigurer security)
throws Exception
security.checkTokenAccess("hasAuthority('USER')");
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code",
"refresh_token", "password").scopes("openid");
【问题讨论】:
这几乎正是我想要做的,你想出解决方案了吗? 【参考方案1】:SpringSecurityFilterChain 应始终排在其他过滤器之前。 如果您想为所有或某些端点添加自己的身份验证,最好的办法是添加您自己的低阶 WebSecurityConfigurerAdapter。如下修改 WebSecurityConfigurerAdapter 子类允许 ResourceServer 使用 jdbc 身份验证管理器:
@Configuration
@Order(-10)
protected static class LoginConfig extends WebSecurityConfigurerAdapter
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
auth.parentAuthenticationManager(authenticationManager).jdbcAuthentication().dataSource(dataSource);
【讨论】:
我遇到了类似的问题并尝试了这种方法/login
在安全方面有效,但返回 404。我怀疑资源服务器和授权服务器之间存在更大的不兼容性以上是关于Spring OAuth @EnableResourceServer 阻止来自 OAuth 服务器的登录页面的主要内容,如果未能解决你的问题,请参考以下文章
Spring OAuth + JWT -- /oauth/token
Spring-OAUTH2.0:调用 /oauth/token 时没有可用资源错误
Spring Security 入门(1-3)Spring Security oauth2.0 指南
Spring-Security OAuth2 设置 - 无法找到 oauth2 命名空间处理程序
使用 Spring boot、Eureka、Zuul、Spring Oauth 创建 OAuth 安全微服务的问题
使用 spring-session 和 spring-cloud-security 时,OAuth2ClientContext (spring-security-oauth2) 不会保留在 Redis