Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.websopti.wotms.auth;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.authentication.AuthenticationProvider;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Component;
- import com.websopti.wotms.entity.User;
- import com.websopti.wotms.enums.ErrorKey;
- import com.websopti.wotms.service.UserService;
- @Component
- public class CustomAuthenticationProvider implements AuthenticationProvider {
- @Autowired
- public UserService userService;
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- String email = authentication.getPrincipal().toString();
- String password = authentication.getCredentials().toString();
- Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
- String username = null;
- try {
- username = userService.authenticate(email, password);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(username != null && !username.equals("0")){
- User user = userService.findByEmail(email);
- if (user != null) {
- authorities.add(new SimpleGrantedAuthority(user.getRole().name()));
- return (new UsernamePasswordAuthenticationToken(user, null, authorities));
- } else {
- User newUser = new User();
- newUser.setName(username);
- newUser.setEmail(email);
- newUser.setPassword(password);
- userService.register(newUser);
- newUser = userService.findById(newUser.getId());
- authorities.add(new SimpleGrantedAuthority(newUser.getRole().name()));
- return (new UsernamePasswordAuthenticationToken(newUser, null, authorities));
- }
- } else {
- throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
- }
- }
- @Override
- public boolean supports(Class<?> authentication) {
- return authentication.equals(UsernamePasswordAuthenticationToken.class);
- }
- }
- package com.websopti.wotms.auth;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Service;
- import com.websopti.wotms.entity.User;
- import com.websopti.wotms.enums.ErrorKey;
- import com.websopti.wotms.service.UserService;
- @Service
- public class AuthenticationService implements UserDetailsService {
- @Autowired
- public UserService userService;
- @Override
- public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
- User user = userService.findByEmail(email);
- if(user != null)
- return user;
- else
- throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement