LDAP的春季认证

时间:2019-05-27 13:25:18

标签: spring spring-security ldap

我正在尝试从Spring配置连接到LDAP数据库。

我有几个可以工作的类,例如“ LDAPConfig”,“ WebSecurityConfig”,“ JwtFilter”。

我还做了更多的事情,一个实现了“ AuthenticationService”的“ AuthentificationServiceImpl”类

@Service
public class AuthentificationServiceImpl implements AuthentificationService {

    static final Logger logger = LoggerFactory.getLogger(AuthentificationServiceImpl.class);

    /**
     * {@link AuthenticationManager}
     */
    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * {@link LdapUserDetailsService}
     */
    @Autowired
    private LdapUserDetailsService ldapUserDetailsService;

    /**
     * {@link TokenProvider}
     */
    @Autowired
    private TokenProvider tokenProvider;

    /**
     * the {@link UserService}
     */
    @Autowired
    private UserService userService;


    /**
     * Authentification method of a user with his {@link CredentialDTO}
     * 
     * @Param - the {@link CredentialDTO} (username and password of the user)
     * @Return - The {@link DocumentDTO}
     */
    public String authentification(CredentialLdap cred) {
        logger.info("Test de creation d'un token");
        if (!cred.getUsername().equals("admin")) {
            String decodedPassword = new String(Base64.getDecoder().decode(cred.getPassword()));
            UsernamePasswordAuthenticationToken authUserPassword = new UsernamePasswordAuthenticationToken(
                    cred.getUsername(), decodedPassword);
            try {
                Authentication auth = authenticationManager.authenticate(authUserPassword);
                SecurityContextHolder.getContext().setAuthentication(auth);
                if (auth.isAuthenticated()) {
                    UserDTO user = userService.getUserByLogin(cred.getUsername());
                    if (user == null) {
                        return null;
                    }
                }
                UserDetails userDetails = ldapUserDetailsService.loadUserByUsername(cred.getUsername());

                JwtUser jwtUser = new JwtUser(userDetails.getUsername(),
                        new Date(Calendar.getInstance().getTimeInMillis() + 3600 * 1000),
                        new ArrayList<>(userDetails.getAuthorities()));

                return tokenProvider.generateToken(jwtUser);

            } catch (BadCredentialsException bce) {
                logger.warn("User not available or not authorized :" + bce.getLocalizedMessage());
                throw bce;
            }
        } else {
            UserDTO user = userService.getUserByLogin(cred.getUsername());
            String decodedPassword = new String(Base64.getDecoder().decode(cred.getPassword()));
            String userDecodedPassword = new String(Base64.getDecoder().decode(user.getPassword()));
            if (decodedPassword.equals(userDecodedPassword)) {
                List<SimpleGrantedAuthority> updatedAuthorities = new ArrayList<SimpleGrantedAuthority>();
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_ADMIN");
                updatedAuthorities.add(authority);

                JwtUser jwtUser = new JwtUser(cred.getUsername(),
                        new Date(Calendar.getInstance().getTimeInMillis() + 3600 * 1000),
                        new ArrayList<>(updatedAuthorities));

                return tokenProvider.generateToken(jwtUser);
            } else {

            }

        }
        return null;
    }

public interface AuthentificationService {

    public String authentification(CredentialLdap credential);

}

当我对Postman进行测试时,出现错误“禁止访问所请求的资源”。

根据正确调试服务器的步骤,获取我的ID和块:

Authentication auth = authenticationManager.authenticate(authUserPassword);

感谢您的帮助,如果需要其他课程,我可以添加它们。

0 个答案:

没有答案
相关问题