为什么我使用Java Config实现AuthenticationProvider不起作用?

时间:2015-11-01 14:44:45

标签: spring-security spring-boot

我尝试将自定义AuthenticationProvider连接到Spring Security配置,如下所示:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired private UserService userService;

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {

    String email = (String) auth.getPrincipal();
    String password = (String) auth.getCredentials();

    System.out.println("[CustomAuthenticationProvider] Authentication try [" + email +"]");

    User user = (User) userService.findByEmail(email);
    if (user == null) {
        throw new BadCredentialsException("[CustomAuthenticationProvider] Authentication Failed!!! Reason: User is not exist: [" + email + "]");
    }

    if (user.getPassword().equals(password)) {
        System.out.println("[CustomAuthenticationProvider] - Authentication Success!!!");

        List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
        roles.add(new SimpleGrantedAuthority("ROLE_USER"));

        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(email, password, roles);
            result.setDetails(user);
            return result;
        } else {
            throw new BadCredentialsException("[CustomAuthenticationProvider] Authentication Failed!!! Reason: Wrong password");
        }
    }

    @Override
    public boolean supports(Class<?> auth) {
        return auth.equals(UsernamePasswordAuthenticationToken.class);
    }
}

这是我的CustomAuthenticationProvider类:

CustomAuthenticationProvider

我打开浏览器并连接到网址:&#34; localhost:8080 / login&#34; 我可以看到登录页面,我输入了id / pw。 点击&#34;登录&#34;按钮,我希望找到CustomAuthenticationProvider的一些日志输出。但是没有日志输出。这意味着我的<security:authentication-manager> <security:authentication-provider ref="customAuthenticationProvider" /> </security:authentication-manager> 无效。

我检查了Google和stackoverflow,但我找不到解决方案。

在此之前,我使用了XML配置,如:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
    </dependency>
</dependencies>

该代码工作正常,但我正在尝试将代码从XML移动到Java配置。

我正在使用Spring Boot ver 1.27。这是我的POM的相关部分:

 $("#itemData td:contains('cellAAA')").html($("#itemName").val());
 $("#itemData td:contains('cellBBB')").html($("#itemNombre").val());

0 个答案:

没有答案
相关问题