【问题标题】:Not being able to provide custom authentication provider for the spring security无法为 Spring Security 提供自定义身份验证提供程序
【发布时间】:2016-07-19 10:47:08
【问题描述】:

我想为 spring 安全性提供一个自定义身份验证提供程序,并且我已经像这样实现了它

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


   }
}

现在我没有任何逻辑,因为我只想看看 spring security 是否真的在使用这个身份验证提供程序。

我的安全配置文件为

@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();
    }
}

我想知道这个

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

是告诉 spring security 使用自定义身份验证管理器的正确方法。

【问题讨论】:

    标签: java spring security spring-mvc spring-security


    【解决方案1】:

    你需要在 Spring 安全配置中添加这个:

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

    auth.authenticationProvider(apiCustomAuthenticationProvider())
    

    提醒一下,如果您返回令牌:

    UsernamePasswordAuthenticationToken("aman", "12345"),

    spring 不会给用户授权。相反,您需要分配角色:

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

    如上所述,您给用户ROLE_USER,然后用户可以使用所有经过身份验证的页面。

    希望有帮助。

    【讨论】:

    • 谢谢它的工作。你能告诉我一个来源,我可以在哪里找到 xml 约定的适当对应物。我搜索了,我只能找到那个@Bean one
    • 欢迎您,请接受答案以帮助他人。是否要创建 xml 而不是 javaconfig?
    • 我做到了。但我是新手,我的支持不会显示,因为我没有足够的声望点。不,我没有。我只想要一个指南,其中存在 xml 配置的 java config 对应项
    • 我的意思是正确的勾号,它低于反对票。您可以单击该勾号以将此答案显示为已接受。实际上,我主要在 spring doc 上学习:docs.spring.io/spring/docs/current/spring-framework-reference/…
    • 很抱歉打扰您,但在我按照您所说的进行操作后,一切正常,但现在只要我自动装配可以帮助我进行身份验证的类并使用它,我就会收到以下错误 java.lang。 NullPointerException org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:192) 你能告诉我为什么会这样吗?我是否也需要一个自定义的用户名密码验证过滤器
    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 2011-02-09
    • 1970-01-01
    • 2016-09-24
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多