2017-04-11 52 views
0

移動移動到基於Java config認證無法通過身份驗證的步驟 可能有人解釋後,如何實現的AuthenticationManager?春天AUTH - 從XML到Java配置

現在我得到

{ 
    "error": "unauthorized", 
    "error_description": "Full authentication is required to access this resource" 
} 

當我通過http://localhost:8080/oauth/token?grant_type=password&[email protected]&password=cant_hack_this&client_id=sso-auth-client&client_secret=mySecret

這裏試圖獲取令牌回購https://github.com/mikesockor/SOFqstn

@SpringBootApplication 
@EnableResourceServer 
@EnableDiscoveryClient 
//@ImportResource({"classpath*:spring-security-oauth2.xml"}) 

如何實現這一點?

<sec:http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationManager" > 
    <sec:intercept-url pattern="/oauth/token" /> 
    <sec:anonymous enabled="true" /> 
    <sec:http-basic entry-point-ref="clientAuthenticationEntryPoint" /> 
    <sec:custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" /> 
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" /> 
</sec:http> 

<sec:http auto-config="true" pattern="/oauth/check_token" create-session="stateless" authentication-manager-ref="authenticationManager"> 
    <sec:intercept-url pattern="/oauth/check_token" access="IS_AUTHENTICATED_FULLY" /> 
    <sec:anonymous enabled="false"/> 
    <sec:http-basic entry-point-ref="clientAuthenticationEntryPoint" /> 
</sec:http> 

<sec:http pattern="/**" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint" 
      access-decision-manager-ref="accessDecisionManager" > 
    <sec:anonymous enabled="false" /> 
    <sec:intercept-url pattern="/**" /> 
    <sec:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> 
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" /> 
</sec:http> 

另外,如果我會盡量

#security.basic.enabled=false 
security.ignored=/** 

越來越

{ 
    "timestamp": 1491919124442, 
    "status": 405, 
    "error": "Method Not Allowed", 
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException", 
    "message": "Request method 'POST' not supported", 
    "path": "/oauth/token" 
} 

回答

0

春天驗證的全配置與啓動,你需要以下

首先,你必須實現了配置的基本類中的春天驗證

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private CustomUserDetailsService userDetailsService; 

    @Autowired 
    private AccountAuthenticatoinProvider accountAuthenticationProvider; 

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

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

} 

接下來,您將需要ResourceServerConfiguration和AuthorizationServerConfiguration

@Configuration 
public class OAuth2ServerConfiguration { 

    private static final String RESOURCE_ID = "restservice"; 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

     ..... 
     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) { 
      // @formatter:off 
      resources 
        .resourceId(RESOURCE_ID).tokenStore(new JwtTokenStore(jwtAccessTokenConverter)); 
      // @formatter:on 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      // @formatter:off 
      http 
        .csrf().disable() 
        .authorizeRequests() 
        .antMatchers("/api/**").authenticated(); 



      // @formatter:on 
     } 

.... 

    } 



    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

     ..... 
     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      // @formatter:off 
      clients 
        .inMemory() 
        .withClient("clientapp") 
        .authorizedGrantTypes("password","refresh_token") 
        .authorities("USER") 
        .scopes("read", "write") 
        .resourceIds(RESOURCE_ID) 
        .secret("123456"); 
      // @formatter:on 
     } 

    } 
..... 

} 

請查看以下混帳回購協議https://github.com/cpapidas/Spring-Boot-OAuth2-JWT-MySQL