【问题标题】:Manual authentication throw CustomAuthenticationProvider (Spring MVC)手动身份验证抛出 CustomAuthenticationProvider (Spring MVC)
【发布时间】:2015-08-04 12:52:34
【问题描述】:

我尝试使用 Spring MVC 使用用户名和密码手动登录

我有 CustomSessionAuthenticationProvider:

  public class CustomSessionAuthenticationStrategy implements AuthenticationProvider {

    @Autowired
    private UserService userService;

    @Override
    public Authentication authenticate(Authentication authentication) {
        String username = authentication.getName();
        String password = (String) authentication.getCredentials();

        User user = userService.loadUserByUsername(username);

        if (user == null) {
            throw new BadCredentialsException("Username not found.");
        }

        if (!password.equals(user.getPassword())) {
            throw new BadCredentialsException("Wrong password.");
        }

        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();

        return new UsernamePasswordAuthenticationToken(user, password, authorities);
    }

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

我将它包含在安全配置中:

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

我不明白如何手动验证,如果我有控制器用户名和密码并从中获取 sessionId 和令牌。

方法验证输入需要验证,但我需要登录名和密码。

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    您不需要控制器进行手动身份验证,如果您已正确配置,spring security 会自动在 /login 处为 post 方法注册一个控制器并为您进行身份验证。请查看此博客文章以获取示例或 spring 安全手册:http://kielczewski.eu/2014/12/spring-boot-security-application/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2019-07-18
    • 2016-10-07
    • 2016-04-30
    • 2022-07-16
    相关资源
    最近更新 更多