Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableAuthorizationServer
- public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
- static final String CLIEN_ID = "clkey";
- static final String CLIENT_SECRET = "dsds876e67ds5s67ddfdf6dfdf767843";
- static final String GRANT_TYPE_PASSWORD = "password";
- static final String AUTHORIZATION_CODE = "authorization_code";
- static final String REFRESH_TOKEN = "refresh_token";
- static final String IMPLICIT = "implicit";
- static final String SCOPE_READ = "read";
- static final String SCOPE_WRITE = "write";
- static final String TRUST = "trust";
- static final int ACCESS_TOKEN_VALIDITY_SECONDS = 1*60*60;
- static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 6*60*60;
- @Autowired
- private TokenStore tokenStore;
- @Autowired
- private UserApprovalHandler userApprovalHandler;
- @Autowired
- private AuthenticationManager authenticationManager;
- @Override
- public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
- configurer
- .inMemory()
- .withClient(CLIEN_ID)
- .secret(CLIENT_SECRET)
- .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT )
- .scopes(SCOPE_READ, SCOPE_WRITE, TRUST)
- .accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS).
- refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
- }
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
- endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
- .authenticationManager(authenticationManager);
- }
- }
- @Component
- public class CustomAuthenticationProvider implements AuthenticationProvider {
- @Autowired
- private UserService auth2;
- @Autowired
- public CustomAuthenticationProvider(CoreUserService coreuserservice) {
- }
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- String password = "";
- String username = authentication.getName();
- if(!auth2.isUserExist(username)) {
- throw new BadCredentialsException("Authentication failed : bad credentials");
- }
- Authentication auth = new UsernamePasswordAuthenticationToken(username, password, auth2.grantAccess());
- return auth;
- }
- @Override
- public boolean supports(Class<?> authentication) {
- return authentication.equals(UsernamePasswordAuthenticationToken.class);
- }
- }
- public ResponseEntity<Map<String, Object>> dologin(String email,String password) throws UsernameNotFoundException {
- this.resetresponse();
- this.responsedata.put("code", "200");
- User user = userdao.findByUsername(email);
- if(user == null)
- this.responsedata.put("code", "1"); //throw new UsernameNotFoundException("Invalid username or password.");
- if(user != null && !encoder.matches(password, user.getPassword()))
- this.responsedata.put("code", "2"); //this.errors.add("2");
- if(! "200".equals(this.responsedata.get("code"))) {
- this.responsedata.put("status", "error");
- }
- else {
- org.springframework.security.core.userdetails.User coreuser = new org.springframework.security.core.userdetails.User(user.getEmail(), "$2a$10$56PJwERx23LPIEPv.gsouOhbn50b2T/AdMV553k0uIi1LflVgD9Y6", grantAccess());
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(coreuser.getUsername(), "", coreuser.getAuthorities());
- SecurityContextHolder.getContext().setAuthentication(authenticationToken);
- //SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- this.responsedata.put("status", "success");
- this.responsedata.put("data",user);
- this.responsedata.put("token",authenticationToken);
- }
- return new ResponseEntity<Map<String, Object>>(this.responsedata,HttpStatus.OK);
- }
Add Comment
Please, Sign In to add comment