Petr Mensik
Petr Mensik

Reputation: 27526

Integration of JSF and Spring Security - ManagedProperty issue

I've got some problem during Spring Security and JSF 2.0 integration. Interception of protected pages and everything else works fine, but when I try to login the user (through my backing bean), it seems that my AuthenticationService which is ManagedProperty of my LoginBean is null. It worked fine when I was using Spring Security 3.7, now I've switched to 3.1.0 and it started to cause me a problems. Here I enclose my LoginBean and AuthenticationService (injecting this interface is causing the 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";
    }
}

this is the AuthenticationService interface and its implementation

public interface AuthenticationService {

    public boolean login(String username, String password);

}

implementation

@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 I tried to use this tutorial http://technology-for-human.blogspot.com/2011/01/jsf-2-with-spring-3-protection-with.html Thanks a lot for the answers!

Upvotes: 0

Views: 1371

Answers (1)

Takunda Dhlakama
Takunda Dhlakama

Reputation: 26

Had a similar problem but you can use @Resource

This allows you to inject the authenticationService.

@Resource(name = "authenticationService")

private AuthenticationService authenticationService;

Upvotes: 1

Related Questions