【问题标题】:Spring security oauth/token returns 401Spring security oauth/token 返回 401
【发布时间】:2018-04-26 08:24:00
【问题描述】:

我正在尝试使用 Spring 和 Spring Security 创建一个简单的应用程序。 当我在正文中使用用户名和密码点击默认 localhost:8080/oauth/token 端点时,如果给定用户不存在,我希望得到一个令牌或错误。 我得到了401。这是为什么? 以下是我的一些配置文件:

@EnableResourceServer
@Configuration
public static class ResourceServer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(PUBLIC_URIS).permitAll();
    }

}

@EnableAuthorizationServer
@Configuration
public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManagerBean;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient(CLIENT_ID)
                .authorizedGrantTypes(GRANT_TYPE)
                .scopes(SCOPE);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManagerBean);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
    }

安全类:

@Configuration
@EnableWebSecurity
public class Security extends WebSecurityConfigurerAdapter {

@Autowired
private UserService usersService;
private PasswordEncoder passwordEncoder = new StandardPasswordEncoder();

@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
    return new SecurityEvaluationContextExtension();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(usersService).passwordEncoder(passwordEncoder);
}

}

还有 web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">


<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>pl.kabat.security.configuration.SimpleCORSFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

【问题讨论】:

  • 您能否将请求(例如作为 cURL 命令)添加到您的问题中?我看到的是您没有在身份验证服务器中配置客户端密码。您在 HTTP Authentication 标头中发送了什么 ID 和密码?
  • @dur: 请求是:POST, localhost:8080/oauth/token { "username":"user", "password":"pass", "grant_type": "password", " client_id”:“连接应用程序”}。恐怕我没有发送任何身份验证标头。
  • 你的问题解决了吗?请发布解决方案并接受您自己的答案。

标签: java spring spring-mvc spring-security oauth


【解决方案1】:

来自 HTTP 规范:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401

“HTTP 401 Unauthorized 客户端错误状态响应代码表示该请求尚未应用,因为它缺少目标资源的有效身份验证凭据。”

换句话说,您得到的是正是身份验证错误。

【讨论】:

  • 是的,但不管我在身体里放了什么,我都会明白这一点。如果我输入正确的用户名和密码,我应该得到一个有效的令牌吗?相反,我每次都得到“需要完全身份验证才能访问此资源”
  • @furry12: 什么是正确的密码?您没有在身份验证服务器配置中配置任何密码。顺便说一句,您必须发送客户端 ID 和客户端密码,而不是用户名和密码。
  • @dur:我认为正确的凭据将是我创建的用户的凭据。 (类用户从 Spring Framework 实现 UserDetails 接口)。我认为它会完成这项工作,但我想我错了。请求是:POST,localhost:8080/oauth/token { "username":"user", "password":"pass", "grant_type": "password", "client_id": "connect-app" }
猜你喜欢
  • 2016-01-30
  • 2019-05-31
  • 2018-11-06
  • 2020-07-24
  • 2019-02-11
  • 2015-05-28
  • 2019-02-21
  • 2017-10-22
  • 2017-10-11
相关资源
最近更新 更多