0

カスタム認証プロバイダーを使用してアプリケーションに春のセキュリティ (Java 構成を使用) を追加したいのですが、機能させることができません。authenticationProvider が適切に構成されていないようです。coz 認証 (Authentication) メソッドにデバッグできず、println は何も出力しません。そして、すべてのリクエストは 403 で応答しました。

誰でもこれで私を助けてください、私は週末全体でこれに打たれました。

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new AuthenticationProvider(){

            @Override
            public Authentication authenticate(Authentication arg0) throws AuthenticationException {
                System.out.println("authenticating...");
                return arg0;
            }

            @Override
            public boolean supports(Class<?> arg0) {
                return true;
            }

         });
    }
}
4

1 に答える 1

1

クラスの authenticate() 関数は、認証が成功した場合は UsernamePasswordAuthenticationToken インスタンスを返す必要があり、それ以外の場合は null を返す必要があります。

**

org.springframework.security.authentication.ProviderManager および configure は、カスタム org.springframework.security.authentication.AuthenticationProvider に設定されます (そのプロバイダーを設定します)。これは、認証メソッドで認証を返す必要があります。これは、GrantedAuthority で設定する必要があります。

**

以下は、権限を設定して認証オブジェクトを返すサンプルの認証マネージャー コードです。

あなたが持っている customAUthenticationProvider クラスを注入してから、configure メソッドでそれを使用できますか?

@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;

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

春のセキュリティのための Web xml マッピングは適切ですか?

   <listener>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>

 <!-- use the springSecurityFilterChain -->
<filter>

          <filter-name>springSecurityFilterChain</filter-name>

        <filter-      class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   <!-- NOTE This does not specify its own configuration it is
     loaded by the ContextLoaderListener instead -->

   <!-- NOTE by default the filter name is used to
     look up the Spring Security Filter Chain if you like you
     can use any filter name you want, but you must specify
     the bean name instead in this instance. Since we use the
     springSecurityFilterChain as the filter name this is not
     necessary
<init-param>
    <param-name>targetBeanName</param-name>
    <param-value>springSecurityFilterChain</param-value>
</init-param> -->
</filter>
<filter-mapping>
   <filter-name>springSecurityFilterChain</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

サンプルの春のセキュリティ プロジェクトは次のとおりです: https://github.com/spring-projects/spring-security-javaconfig

于 2015-03-08T12:46:31.680 に答える