7

I trying authenticate user in Spring Security application via oAuth. I'm already received token and user's data.

How can I authenticate user manually without password and classic login form? Thank you.

1 Answer 1

22

Something like this:

Authentication authentication =  new UsernamePasswordAuthenticationToken(person, null, person.getAuthorities());
log.debug("Logging in with {}", authentication.getPrincipal());
SecurityContextHolder.getContext().setAuthentication(authentication);

Where person is your UserDetailsBean object.

Sign up to request clarification or add additional context in comments.

1 Comment

Please note that this will work only if your signIn path uses security mapping of following type <s:intercept-url pattern="/signin/**" access="" /> In the newer versions of spring security there is a better and faster way of skipping security for eg. <s:http security="none" pattern="/assets/**"/> with the new format the given solution will not work as there is no SecurityContextHolder created in later scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.