Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. 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.
  2.  
  3.  
  4. package com.mycom.config.security;
  5.  
  6. import java.util.Collection;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9.  
  10. import javax.security.auth.callback.CallbackHandler;
  11. import javax.security.auth.login.LoginContext;
  12.  
  13. import org.springframework.beans.factory.annotation.Autowired;*/
  14. import java.util.Collection;
  15.  
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.context.annotation.Configuration;
  18. import org.springframework.security.authentication.AuthenticationProvider;
  19. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  20. import org.springframework.security.core.Authentication;
  21. import org.springframework.security.core.GrantedAuthority;
  22.  
  23. import com.mycom.entity.User;
  24. import com.mycom.service.UserService;
  25. import org.springframework.security.core.GrantedAuthority;
  26. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  27. import org.springframework.security.core.userdetails.UserDetails;
  28.  
  29.  
  30. @Configuration
  31. public class MyAppAuthenticationProvider implements AuthenticationProvider {
  32.  
  33. private static final String SECURITY_DOMAIN = "MyApp-ldapdomain";
  34.  
  35. @Autowired
  36. private UserService userService;
  37.  
  38. @Override
  39. public Authentication authenticate(Authentication authentication) {
  40.  
  41. String name = authentication.getName();
  42. String password = authentication.getCredentials().toString();
  43.  
  44. try {
  45. CallbackHandler handler = new UserPasswordHandler(name, password);
  46. LoginContext lc = new LoginContext(SECURITY_DOMAIN, handler);
  47. lc.login();
  48.  
  49. if(lc.getSubject() != null) {
  50.  
  51. User user = userService.loadUserByUsername(name);
  52.  
  53. Collection<? extends GrantedAuthority> roles = null;
  54.  
  55. if(!user.getAuthorities().isEmpty()) {
  56. roles = user.getAuthorities();
  57. }
  58.  
  59. return new UsernamePasswordAuthenticationToken(name, password,roles);
  60.  
  61. } catch (Exception e) {
  62. return null;
  63. }
  64. }
  65.  
  66. @Override
  67. public boolean supports(Class<?> authentication) {
  68. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement