Guest User

Untitled

a guest
Mar 8th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  4.  
  5. static final String CLIEN_ID = "clkey";
  6. static final String CLIENT_SECRET = "dsds876e67ds5s67ddfdf6dfdf767843";
  7. static final String GRANT_TYPE_PASSWORD = "password";
  8. static final String AUTHORIZATION_CODE = "authorization_code";
  9. static final String REFRESH_TOKEN = "refresh_token";
  10. static final String IMPLICIT = "implicit";
  11. static final String SCOPE_READ = "read";
  12. static final String SCOPE_WRITE = "write";
  13. static final String TRUST = "trust";
  14. static final int ACCESS_TOKEN_VALIDITY_SECONDS = 1*60*60;
  15. static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 6*60*60;
  16.  
  17. @Autowired
  18. private TokenStore tokenStore;
  19.  
  20. @Autowired
  21. private UserApprovalHandler userApprovalHandler;
  22.  
  23. @Autowired
  24. private AuthenticationManager authenticationManager;
  25.  
  26. @Override
  27. public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
  28.  
  29. configurer
  30. .inMemory()
  31. .withClient(CLIEN_ID)
  32. .secret(CLIENT_SECRET)
  33. .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT )
  34. .scopes(SCOPE_READ, SCOPE_WRITE, TRUST)
  35. .accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS).
  36. refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
  37. }
  38.  
  39. @Override
  40. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  41. endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
  42. .authenticationManager(authenticationManager);
  43. }
  44. }
  45.  
  46. @Component
  47. public class CustomAuthenticationProvider implements AuthenticationProvider {
  48.  
  49.  
  50. @Autowired
  51. private UserService auth2;
  52.  
  53. @Autowired
  54. public CustomAuthenticationProvider(CoreUserService coreuserservice) {
  55. }
  56.  
  57. @Override
  58. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  59. String password = "";
  60. String username = authentication.getName();
  61.  
  62. if(!auth2.isUserExist(username)) {
  63. throw new BadCredentialsException("Authentication failed : bad credentials");
  64. }
  65.  
  66. Authentication auth = new UsernamePasswordAuthenticationToken(username, password, auth2.grantAccess());
  67. return auth;
  68. }
  69.  
  70. @Override
  71. public boolean supports(Class<?> authentication) {
  72. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  73. }
  74.  
  75. }
  76.  
  77. public ResponseEntity<Map<String, Object>> dologin(String email,String password) throws UsernameNotFoundException {
  78. this.resetresponse();
  79. this.responsedata.put("code", "200");
  80. User user = userdao.findByUsername(email);
  81. if(user == null)
  82. this.responsedata.put("code", "1"); //throw new UsernameNotFoundException("Invalid username or password.");
  83. if(user != null && !encoder.matches(password, user.getPassword()))
  84. this.responsedata.put("code", "2"); //this.errors.add("2");
  85. if(! "200".equals(this.responsedata.get("code"))) {
  86. this.responsedata.put("status", "error");
  87. }
  88. else {
  89.  
  90. org.springframework.security.core.userdetails.User coreuser = new org.springframework.security.core.userdetails.User(user.getEmail(), "$2a$10$56PJwERx23LPIEPv.gsouOhbn50b2T/AdMV553k0uIi1LflVgD9Y6", grantAccess());
  91. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(coreuser.getUsername(), "", coreuser.getAuthorities());
  92. SecurityContextHolder.getContext().setAuthentication(authenticationToken);
  93. //SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  94.  
  95. this.responsedata.put("status", "success");
  96. this.responsedata.put("data",user);
  97. this.responsedata.put("token",authenticationToken);
  98. }
  99. return new ResponseEntity<Map<String, Object>>(this.responsedata,HttpStatus.OK);
  100. }
Add Comment
Please, Sign In to add comment