![](/img/trans.png)
[英]Spring Security OAuth - Full authentication is required to access this resource
[英]Spring-Security-Oauth2: Full authentication is required to access this resource
我正在尝试将spring-security-oauth2.0
与基于 Java 的配置一起使用。 我的配置已经完成,但是当我在 tomcat 上部署应用程序并点击/oauth/token
url 获取访问令牌时, Oauth
生成以下错误:
<oauth>
<error_description>Full authentication is required to access this resource</error_description>
<error>unauthorized</error>
</oauth>
我的配置在Git hub 上,请点击链接
代码比较大,参考git。 我正在使用 chrome postman 客户端发送请求。 以下是我的要求。
POST /dummy-project-web/oauth/token HTTP/1.1
Host: localhost:8081
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=abc%40gmail.com&client_secret=12345678
错误就像,该 URL 由Oauth
,但在配置中,我授予访问此 URL 的所有权限。 这个问题究竟是什么?
默认情况下, client_id
和client_secret
应该放在 Authorization 标头中,而不是 form-urlencoded 正文中。
client_id
和client_secret
,它们之间有一个冒号: abc@gmail.com:12345678
。YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
Authorization: Basic YWJjQGdtYWlsLmNvbToxMjM0NTY3OA==
默认情况下,Spring OAuth 需要基本的 HTTP 身份验证。 如果您想使用基于 Java 的配置将其关闭,则必须允许对客户端进行表单身份验证,如下所示:
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
}
原因是默认情况下/oauth/token
端点是通过Basic Access Authentication保护的。
您需要做的就是将Authorization
标头添加到您的请求中。
您可以通过发出以下命令,使用curl
类的工具轻松测试它:
curl.exe --user abc@gmail.com:12345678 http://localhost:8081/dummy-project-web/oauth/token?grant_type=client_credentials
使用 Spring OAuth 2.0.7-RELEASE,以下命令对我有用
curl -v -u abc@gmail.com:12345678 -d "grant_type=client_credentials" http://localhost:9999/uaa/oauth/token
它也适用于 Chrome POSTMAN,只需确保您在“基本身份验证”选项卡中设置客户端和密码,将方法设置为“POST”并在“表单数据”选项卡中添加授权类型。
您应该预先验证令牌 apis "/oauth/token"
扩展ResourceServerConfigurerAdapter
并覆盖configure function
来做到这一点。
例如:
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/oauth/token").permitAll().
anyRequest().authenticated();
我遇到了同样的问题,但我用以下课程解决了这个问题:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class OAuthSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
这是不可思议但真实的。
csrf 过滤器默认启用,它实际上会阻止任何不包含 de csrf 令牌的 POST、PUT 或 DELETE 请求。
如果是这样,则允许任何 HTTP 方法:
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
/**
* Allow POST, PUT or DELETE request
*
* NOTE: csrf filter is enabled by default and it actually blocks any POST, PUT or DELETE requests
* which do not include de csrf token.
*/
http.csrf().disable()
}
如果您正在获得 401,最直观的做法是认为在请求中您没有 Auth 或者您在标题中缺少有关授权的内容。
但显然有一个内部函数正在过滤使用 POST 的 HTTP 方法并返回 401。修复它后,我认为这是状态代码的缓存问题,但显然不是。
GL
在application.properties
设置management.security.enabled=false
为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.