JSF和Spring Security的集成 - ManagedProperty问题

时间:2011-11-10 14:59:32

标签: jsf spring-security

我在Spring Security和JSF 2.0集成期间遇到了一些问题。拦截受保护的页面和其他一切工作正常,但是当我尝试登录用户(通过我的支持bean)时,似乎我的AuthenticationService是我的LoginBean的ManagedProperty为null。当我使用Spring Security 3.7时,它工作正常,现在我已切换到3.1.0,它开始给我带来问题。在这里,我附上了我的LoginBean和AuthenticationService(注入此接口导致NullPointerException)。

//LoginBean
@ManagedProperty(value="#{authenticationService}")  
private AuthenticationService authenticationService;

public String login() {
    boolean success = authenticationService.login(name, password);
    if(success) {
        return "/faces/signed/home.xhtml?faces-redirect=true";
    }
    else {
        return "/faces/accessDenied.xhtml?faces-redirect=true";
    }
}

这是AuthenticationService接口及其实现

public interface AuthenticationService {

    public boolean login(String username, String password);

}

实施

@Service("authenticationService")
public class AuthenticationServiceImpl implements AuthenticationService {

@Resource
AuthenticationManager authenticationManager;

@Override
public boolean login(String username, String password) {
    try{
        Authentication authenticate = 
                authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                        username, password));
        if(authenticate.isAuthenticated()) {
            SecurityContextHolder.getContext().setAuthentication(authenticate);
            return true;
        }
    }
    catch (AuthenticationException e) {
        System.out.println("chyba");
    }
    return false;
}

btw我尝试使用本教程http://technology-for-human.blogspot.com/2011/01/jsf-2-with-spring-3-protection-with.html非常感谢您的答案!

1 个答案:

答案 0 :(得分:1)

遇到类似问题,但您可以使用@Resource

这允许您注入authenticationService。

@Resource(name = "authenticationService")

private AuthenticationService authenticationService;