2016-03-31 10 views
2

春のセキュリティのためのカスタム認証プロバイダを提供することができないと私はこのは、私は春のセキュリティのためのカスタム認証プロバイダを持ちたい

@Component 
public class ApiCustomAuthenticationProvider implements AuthenticationProvider { 
@Override 
public Authentication authenticate(Authentication authentication) throws AuthenticationException { 

System.out.println("ahsgdvjasdhgjasjdh"); 
return new UsernamePasswordAuthenticationToken("aman", "12345"); 
} 
@Override 
public boolean supports(Class<?> authentication) { 
    return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); 


    } 
} 

のようにそれを実装しています私は春のセキュリティが実際にこの認証プロバイダを使用しているかどうかを確認したいだけです。私はこの

@Bean 
public ApiCustomAuthenticationProvider apiCustomAuthenticationProvider() { 
    return new ApiCustomAuthenticationProvider(); 

は、カスタム認証マネージャを使用するように春のセキュリティを伝えるの正しい方法であるかどうかを知りたい

@Configuration 
@EnableWebSecurity 
//@ImportResource("classpath:/security/spring_saml_sso_security.xml") 

public class SecurityConfig extends WebSecurityConfigurerAdapter { 
/*@Autowired 
MetadataGeneratorFilter metadataGeneratorFilter; 
@Autowired 
FilterChainProxy samlFilter; 
@Autowired 
SAMLEntryPoint samlEntryPoint; 
*/ 

@Autowired 
private CustomUserDetailsService customUserDetailsService; 




@Override 
protected void configure(HttpSecurity http) throws Exception { 

    try { 
     http 
     .csrf().disable() 
     .authorizeRequests() 
     .antMatchers("/static/**").permitAll() 
     .antMatchers("/settings/api/**").permitAll() 
     .antMatchers("/api/**").permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .formLogin() 
     .loginPage("/login").permitAll() 
     .loginProcessingUrl("/login") 
     // .usernameParameter("username").passwordParameter("password") 
     .defaultSuccessUrl("/index",true) 
     .and() 
     .httpBasic(); 
    // .defaultSuccessUrl("/", true); 
} catch (Exception e) { 
     // TODO Auto-generated catch block 
     System.out.println("sadhiasdniaaaaaaaaaaaaaaaa:"); 
     e.printStackTrace(); 
    } 
} 
@Bean 
public ApiCustomAuthenticationProvider apiCustomAuthenticationProvider() { 
    return new ApiCustomAuthenticationProvider(); 
    } 
} 

として

は、私は私のセキュリティ設定ファイルを持っています。

答えて

1

あなたは春のセキュリティの設定でこれを追加する必要があります:あなたはトークンを返す場合、

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(new ApiCustomAuthenticationProvider()); 
} 

または

auth.authenticationProvider(apiCustomAuthenticationProvider()) 

リマインダーとして:

UsernamePasswordAuthenticationToken("aman", "12345")

春ユーザーに許可を与えません。ユーザーROLE_USERを与え、その後、ユーザ認証されたすべてのページを使用することができますされ、上述したように

List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
UsernamePasswordAuthenticationToken("aman", "12345",grantedAuths) ; 

:代わりに、役割を割り当てる必要があります。

ヘルプが必要です。

+0

ありがとうございました。あなたは、私がXMLの慣習に適切な対応を見つけることができるソースを教えてくれますか?私は検索したが、私はその@Beanを見つけることができただけだ –

+0

ようこそ、他人を助けるために答えを受け入れてください。 javaconfigではなくxmlを作成しますか? – FreezY

+0

私はそれをしました。しかし、私は新しいものであり、私のアップアップは、私が十分な評判ポイントを持っていないので、表示されません。いいえ、私はしません 。私はちょうどxml構成の対応するjava設定の部分が存在するガイドを欲しいです。 –

関連する問題