cn.net.withub.demo.bootsec.hello.security.CustomAuthenticationProvider.java Source code

Java tutorial

Introduction

Here is the source code for cn.net.withub.demo.bootsec.hello.security.CustomAuthenticationProvider.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.withub.demo.bootsec.hello.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;

/**
 * ???Spring????
 *
 * @author Diluka
 */
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private CustomUserDetailsService userDetailsService;

    /**
     * SpringMD5
     */
    @Autowired
    private Md5PasswordEncoder md5PasswordEncoder;

    @Transactional
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        String username = token.getName(); //???
        //?
        UserDetails userDetails = null;
        if (username != null) {
            userDetails = userDetailsService.loadUserByUsername(username);
        }

        if (userDetails == null) {
            return null;//null??
            //throw new UsernameNotFoundException("??/?");
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("?");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("?");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("??");
        } else if (!userDetails.isCredentialsNonExpired()) {
            throw new LockedException("?");
        }

        //??
        String encPass = userDetails.getPassword();

        //authentication?credentials
        if (!md5PasswordEncoder.isPasswordValid(encPass, token.getCredentials().toString(), null)) {
            throw new BadCredentialsException("Invalid username/password");
        }

        //?
        return new UsernamePasswordAuthenticationToken(userDetails, encPass, userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return UsernamePasswordAuthenticationToken.class.getName().equals(authentication.getName());
    }

}