Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- As I am setting up login, and authentication is handled at security config side, so once it is successful it shows home page. Hence no controller mentioned.
- package com.mycom.config.security;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Set;
- import javax.security.auth.callback.CallbackHandler;
- import javax.security.auth.login.LoginContext;
- import org.springframework.beans.factory.annotation.Autowired;*/
- import java.util.Collection;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.authentication.AuthenticationProvider;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.GrantedAuthority;
- import com.mycom.entity.User;
- import com.mycom.service.UserService;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- @Configuration
- public class MyAppAuthenticationProvider implements AuthenticationProvider {
- private static final String SECURITY_DOMAIN = "MyApp-ldapdomain";
- @Autowired
- private UserService userService;
- @Override
- public Authentication authenticate(Authentication authentication) {
- String name = authentication.getName();
- String password = authentication.getCredentials().toString();
- try {
- CallbackHandler handler = new UserPasswordHandler(name, password);
- LoginContext lc = new LoginContext(SECURITY_DOMAIN, handler);
- lc.login();
- if(lc.getSubject() != null) {
- User user = userService.loadUserByUsername(name);
- Collection<? extends GrantedAuthority> roles = null;
- if(!user.getAuthorities().isEmpty()) {
- roles = user.getAuthorities();
- }
- return new UsernamePasswordAuthenticationToken(name, password,roles);
- } catch (Exception e) {
- return null;
- }
- }
- @Override
- public boolean supports(Class<?> authentication) {
- return authentication.equals(UsernamePasswordAuthenticationToken.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement