重定向到登录 AuthServer Spring 找不到 oauth2
Redirects to login AuthServer Spring oauth2 not found
我在一个应用程序中有一个带有 spring 的 AuthorizationServer,我想在我的 api 其余部分使用 Bearer 令牌授权和验证调用。
通信顺利(我认为)但是当令牌有效时显示:
{
"timestamp": 1466606797251,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/authorizationServer/login"
}
当令牌无效时说:
{
"error": "invalid_token",
"error_description": "Access token expired: cc281c6a-aeef-401c-879a-624c94058c7b"
}
为什么找不到节目?
编辑:
我的休息服务很简单:
申请:
@SpringBootApplication
@EnableOAuth2Sso
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
终点:
@Controller
public class HelloController {
@RequestMapping(value = "/hello",
method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
return "Hello User!";
}
}
配置:
security:
oauth2:
client:
clientId: client
clientSecret: secret
accessTokenUri: http://localhost:8080/api/v1/secure/oauth/token
userAuthorizationUri: http://localhost:8080/api/v1/secure/oauth/authorize
clientAuthenticationScheme: header
resource:
tokenInfoUri: http://localhost:8080/api/v1/secure/oauth/check_token
server:
port: 8082
contextPath: /api/v1/auth
正如我的评论所说:
收到传入请求时,Spring 首先检查令牌有效性。
如果令牌无效,您将收到 invalid_token
消息。
如果令牌有效,Spring 将请求转发给控制器。但是在你的情况下,没有 url 映射到 /login
,所以你遇到了 404.
在检查 URL 是否存在之前检查令牌有效性是一种需要的行为,这就是您在不存在的 URL 上遇到 invalid_token
的原因。
它对我有用添加:
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.checkTokenAccess("permitAll()");
}
在 AuthServer 上和:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Order(2)
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
;
}
@Order(1)
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/api/**");
}
}
在资源服务器上
我在一个应用程序中有一个带有 spring 的 AuthorizationServer,我想在我的 api 其余部分使用 Bearer 令牌授权和验证调用。
通信顺利(我认为)但是当令牌有效时显示:
{
"timestamp": 1466606797251,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/authorizationServer/login"
}
当令牌无效时说:
{
"error": "invalid_token",
"error_description": "Access token expired: cc281c6a-aeef-401c-879a-624c94058c7b"
}
为什么找不到节目?
编辑:
我的休息服务很简单:
申请:
@SpringBootApplication
@EnableOAuth2Sso
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
终点:
@Controller
public class HelloController {
@RequestMapping(value = "/hello",
method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
return "Hello User!";
}
}
配置:
security:
oauth2:
client:
clientId: client
clientSecret: secret
accessTokenUri: http://localhost:8080/api/v1/secure/oauth/token
userAuthorizationUri: http://localhost:8080/api/v1/secure/oauth/authorize
clientAuthenticationScheme: header
resource:
tokenInfoUri: http://localhost:8080/api/v1/secure/oauth/check_token
server:
port: 8082
contextPath: /api/v1/auth
正如我的评论所说:
收到传入请求时,Spring 首先检查令牌有效性。
如果令牌无效,您将收到 invalid_token
消息。
如果令牌有效,Spring 将请求转发给控制器。但是在你的情况下,没有 url 映射到 /login
,所以你遇到了 404.
在检查 URL 是否存在之前检查令牌有效性是一种需要的行为,这就是您在不存在的 URL 上遇到 invalid_token
的原因。
它对我有用添加:
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.checkTokenAccess("permitAll()");
}
在 AuthServer 上和:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Order(2)
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
;
}
@Order(1)
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/api/**");
}
}
在资源服务器上