2017-11-13 9 views
0

私は春と春のセキュリティで簡単なアプリケーションを作成しようとしています。 身体にあるデフォルトのlocalhost:8080/oauth/tokenエンドポイントにユーザー名とパスワードを入力すると、トークンを取得するか、指定されたユーザーが存在しない場合はエラーが発生することが予想されます。 私は401を得る。何故ですか?春のセキュリティoauth /トークンを返す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(); 
    } 

Securityクラス:

@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> 

+0

質問に(たとえば、cURLコマンドとして)リクエストを追加できますか?私が見ているのは、認証サーバーにクライアントシークレットを設定していないということです。どのようなIDと秘密をあなたのHTTPの 'Authentication'ヘッダに送りましたか? – dur

+0

@dur:リクエストはPOST、localhost:8080/oauth/token {"username": "user"、 "password": "pass"、 "grant_type": "password"、 "client_id": "connect-app "}。 私は認証ヘッダーを送信していません。 – furry12

答えて

0

HTTPから は、ここに私の設定ファイルの一部です仕様:012

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

は、「HTTP 401不正なクライアントエラーステータス応答コードは、ターゲット・リソースの有効な認証資格情報がないため、要求が適用されていないことを示しています。」

つまり、取得するのは正確にです。認証エラーです。

+0

はい、私は体に何を入れたかに関係なくそれを取得します。私は正しいユーザー名とパスワードを入れたら、私は正しいトークンを取得する必要がありますか?代わりに、私は毎回 – furry12

+0

@ furry12: "このリソースにアクセスするには完全な認証が必要です"というメッセージが表示されます。正しいパスワードは何ですか?認証サーバー構成ではパスワードを設定していませんでした。ところで、ユーザーIDとパスワードではなく、クライアントIDとクライアントシークレットを送信する必要があります。 – dur

+0

@ dur:私は正しい資格情報が私が作成したユーザーのものだと思った。 (クラス・ユーザーはSpring FrameworkからUserDetailsインターフェースを実装しています)。私はそれが仕事をするだろうと思ったが、私は間違っていたと思う。 要求がある: POSTは、http:// localhostを:8080 /にOAuth /トークン/ { \t "ユーザー名": "ユーザー"、 \t "パスワード": "パス"、 \t "grant_type": "password"、 \t "client_id": "connect-app" } – furry12

関連する問題