All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yanyun.auth.config.AuthAnnotationConfiguration Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.yanyun.auth.config;

import com.yanyun.auth.dto.ResultDto;
import com.yanyun.auth.exception.TokenLegalException;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class AuthAnnotationConfiguration {
    /**
     * 定义拦截操作,做前置拦截,如果有注解@RequireCheck的方法会进行远端token校验
     */
    @Aspect
    @Component
    class RequireCheckAspect {
        //切点
        @Pointcut("@annotation(com.yanyun.auth.annotation.RequireCheck)")
        public void pointCut() {
        }

        @Value("${authentication.authUrl}")
        private String authUrl;
        @Autowired
        private HttpServletRequest request;
        @Autowired
        private RestTemplate restTemplate;

        //前置拦截
        @Before("pointCut()")
        public void before() {
            //因为access_token可能放在请求的后边,也可能放在header中,则可以进行拿取
            String access_token = request.getParameter("access_token");
            if (StringUtils.isEmpty(access_token)) {
                //那么从请求头中获取
                String authorization = request.getHeader("Authorization");
                //如果仍未取到
                if(StringUtils.isEmpty(access_token)){
                    //抛出异常
                    throw new TokenLegalException("token校验不合法");
                }
                access_token = authorization.substring(6).trim();
            }

            //如果access_token不合法,那么抛出异常
            if (!isLegal(access_token).getData()) {
                throw new TokenLegalException("token校验不合法");
            }

        }


        private ResultDto isLegal(String access_token) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity httpEntity = new HttpEntity(headers);
            ResultDto resultDto = restTemplate.postForObject(authUrl + "/check_token?token="+access_token,httpEntity,ResultDto.class);
            return resultDto;
        }


    }

    /**
     * 定义校验异常后处理
     */
    @ControllerAdvice
    class TokenLegalAdvice {

        @ExceptionHandler(TokenLegalException.class)
        @ResponseBody
        public Map tokenLegleException(TokenLegalException exception){
            Map map = new HashMap();
            map.put("code","403");
            map.put("msg","token校验不合法");
            return map;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy