springboot 통합 shiro, JWT 무 상태 인증 실현

71803 단어 Shiro
jwt 와 shiro 의존 패키지 도입
<!-- shiro   -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-web-starter</artifactId>
    <version>1.4.0</version>
</dependency>

<!-- JWT   -->
<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.9.0</version>
</dependency>

2. JWT 도구 류
package com.xzht.uitls;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

/**
 * JWT   
 */
public class JWTUtil {

    //     
    private static final long EXPIRE_TIME = 60 * 5 * 1000;

    //   
    private static final String SECRET = "secret";

    /**
     *   token
     * @param time          
     * @param username     
     * @return
     */
    public static String createToken (String username) {
        time = (time == 0) ? EXPIRE_TIME : time;

        try {
            //     
            Date date = new Date(System.currentTimeMillis() + time);
            //   
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            return JWT.create()
                    .withClaim("username", username)
                    //     
                    .withExpiresAt(date)
                    //      JWT,            
                    .sign(algorithm);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *   token    
     * @param token
     * @param
     * @return
     */
    public static boolean verify(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            //  token    username  
            JWTVerifier verifier = JWT.require(algorithm).build();
            //   token
            verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     *   token    ,  secret      
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }


}


3. 사용자 정의 token 포장 류
package com.xzht.bean;

import org.apache.shiro.authc.AuthenticationToken;

/**
 * token
 */
public class JWTToken implements AuthenticationToken {

    private String token;

    public JWTToken(String token) {
        this.token = token;
    }

    @Override
    public Object getPrincipal() {
        return token;
    }

    @Override
    public Object getCredentials() {
        return token;
    }
}


4. shiro 기본 session 클래스 사용 하지 않 기
package com.xzht.handler;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;

/**
 *   shiro   session
 */
public class StatelessDefaultSubjectFactory extends DefaultWebSubjectFactory {
    @Override
    public Subject createSubject(SubjectContext context) {
        context.setSessionCreationEnabled(false);
        return super.createSubject(context);
    }
}


5. shiro 프로필

package com.xzht.configs;

import com.xzht.filter.JWTFilter;
import com.xzht.shiro.MyUserRealm;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xzht.handler.StatelessDefaultSubjectFactory;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean(name = "userRealm")
    public MyUserRealm userRealm(@Qualifier("hashedCredentialsMatcher")
                                         CredentialsMatcher matcher) {

        MyUserRealm userRealm = new MyUserRealm();
        //userRealm.setCredentialsMatcher(matcher);
        return userRealm;
    }

	/**
	 *     
	 */
    @Bean(name = "hashedCredentialsMatcher")
    public CredentialsMatcher hashedCredentialsMatcher() {
        CredentialsMatcher credentialsMatcher =
                new CredentialsMatcher();

        return credentialsMatcher;
    }

    /**
     *    token   ,           token,   token   login,    Realm    
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //              jwt
        Map<String, Filter> jwtFilterMap = new LinkedHashMap<>();
        //        JWT   
        jwtFilterMap.put("jwt", new JWTFilter());
        bean.setFilters(jwtFilterMap);

        //    SecurityManager
        bean.setSecurityManager(securityManager);

        //         Url
        //bean.setSuccessUrl("/main");
        //       Url
        //bean.setLoginUrl("/toLogin");
        //        Url
        //bean.setUnauthorizedUrl("/error/unAuth");

        /**
         * anon:       
         * authc:       
         * user:  rememberMe   
         * perms:       
         * role:         
         **/

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/*.html","anon");
        filterMap.put("/login","anon"); //           

        //            JWT Filter
        filterMap.put("/**", "jwt");

        bean.setFilterChainDefinitionMap(filterMap);
        return bean;
    }

    /**
     *    securityManager
     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") MyUserRealm userRealm) {

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //   realm.
        securityManager.setRealm(userRealm);

        //   shiro   session
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);
        securityManager.setSubjectFactory(subjectFactory());

        return securityManager;
    }

    /**
     * 
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
        aasa.setSecurityManager(securityManager);
        return aasa;
    }

    /**
     *       
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        //     cglib,                  
        daap.setProxyTargetClass(true);
        return daap;
    }

    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public StatelessDefaultSubjectFactory subjectFactory() {
        StatelessDefaultSubjectFactory statelessDefaultSubjectFactory = new StatelessDefaultSubjectFactory();
        return statelessDefaultSubjectFactory;
    }

}



6. 사용자 정의 JWTtoken 필터
package com.xzht.filter;

import com.xzht.bean.JWTToken;
import com.xzht.uitls.JWTUtil;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 *       ,  token BasicHttpAuthenticationFilter
 */
public class JWTFilter extends FormAuthenticationFilter {

    private Logger logger = LoggerFactory.getLogger(JWTFilter.class);

    /**
     *     token,  token    ,      
     * @param request
     * @param response
     * @param mappedValue
     * @return
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        //        token
        String token = httpServletRequest.getHeader("token");

        //          token
        if (token != null) {
            try {
                executeLogin(request, response);
            } catch (Exception e) {
                //   token  ,  false,  onAccessDenied  
                return false;
            }
        } else {
            return false;
        }
        //         token,                 ,    token,    true
        return true;
    }

    /**
     *       
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
   @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        //       token
        String token = httpServletRequest.getHeader("token");
        JWTToken jwtToken = new JWTToken(token);
        //    realm      ,             
        getSubject(request, response).login(jwtToken);
        //              ,  true
        return true;
    }

    /**
     *        
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        //           option  ,     option          
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(httpServletRequest, httpServletResponse);
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        System.out.println("isAccessAllowed false     ");
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
        httpServletResponse.getWriter().write("{\"code:401\"}");
        return false;
    }

}


realm
package com.xzht.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzht.bean.JWTToken;
import com.xzht.entity.User;
import com.xzht.service.UserService;
import com.xzht.uitls.JWTUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyUserRealm extends AuthorizingRealm {
    private Logger logger = LoggerFactory.getLogger(MyUserRealm.class);

    @Autowired
    private UserService userService;

    /**
     *        ,     
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     *   
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.info("       ");
        String username = JWTUtil.getUsername(principals.toString());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        return null;
    }

    /**
     *   
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
     
        String token = (String)authenticationToken.getCredentials();
        //  token     username,        
        String username = JWTUtil.getUsername(token);
        if (username == null || !JWTUtil.verify(token)) {
            throw new AuthenticationException("token    ");
        }
		
        User user = userService.getOne(new QueryWrapper<User>().eq("username", username));

        if (!user.getUsername().equals(username)) {
            System.out.println("     ");
            return null;
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(token, token, getName());

        return authenticationInfo;
    }
}


등록
	@Autowired
    private UserService userService;

    /**
     *   
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public ResultUtil login (String username, String password) {

        User user = userService.getOne(new QueryWrapper<User>().eq("username", username));

        if (user.getPassword() == null) {
            return ResultUtil.error(1,"     ");
        } else if (!BCrypt.checkpw(password, user.getPassword())) {
            return ResultUtil.error(1,"    ");
        } else {
            String token = JWTUtil.createToken(username);
            logger.info("  token:" + token);
            //  token     
            return ResultUtil.success(token);
        }

    }

친구 들 에 게 도움 이 된다 면 댓 글 을 남 겨 주세요.

좋은 웹페이지 즐겨찾기