java - Not being able to provide custom authentication provider for the spring security -
i want have custom authentication provider spring security , have implemented
@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));      } } right don't have logic want see if spring security using authentication provider .
i have security config file as
@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();     } } i want know if
@bean public apicustomauthenticationprovider apicustomauthenticationprovider() {     return new apicustomauthenticationprovider(); is correct way of telling spring security use custom authentication manager .
you need add in spring security config:
@autowired public void configureglobal(authenticationmanagerbuilder auth) throws exception {     auth.authenticationprovider(new apicustomauthenticationprovider()); } or
auth.authenticationprovider(apicustomauthenticationprovider()) and reminder, if return token :
usernamepasswordauthenticationtoken("aman", "12345"), 
spring not give authorization user. instead need assign role :
list<grantedauthority> grantedauths = new arraylist<grantedauthority>(); grantedauths.add(new simplegrantedauthority("role_user")); usernamepasswordauthenticationtoken("aman", "12345",grantedauths) ; as stated above,you giving user role_user , user can use authenticated page. 
hope help.
Comments
Post a Comment