1.token.java
package com.hbsc.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by duyu on 2019/1/3.
* Token注解类
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Token {
/**
* 生成Token
* @return
*/
boolean save() default false;
/**
* 删除Token
* @return
*/
boolean remove() default false;
}
2.webappconfigurer.java
package com.hbsc.config;
import com.github.pagehelper.PageHelper;
import com.hbsc.interceptor.CostTimeInterceptor;
import com.hbsc.interceptor.LoginHandlerInterceptor;
import com.hbsc.interceptor.RightsHandlerInterceptor;
import com.hbsc.interceptor.TokenHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Created by xudong on 2017-10-18.
*/
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
@Bean
public RightsHandlerInterceptor rightsHandlerInterceptor() {
return new RightsHandlerInterceptor();
}
@Bean
public LoginHandlerInterceptor loginHandlerInterceptor(){ return new LoginHandlerInterceptor();}
@Bean
public CostTimeInterceptor costTimeInterceptor() {
return new CostTimeInterceptor();
}
/**
* token拦截器
* @return
*/
@Bean
public TokenHandlerInterceptor tokenHandlerInterceptor(){return new TokenHandlerInterceptor();}
public static List<String> allPowerList;
@Override
public void addInterceptors(InterceptorRegistry registry) {
List<String> allPowerList= addInterceptorRoad();
registry.addInterceptor(loginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/changeSessionLanauage","/","/analysis/**","/checkInterface/**","/error","/indexUser/UpdatePassword","/login/getVerify","/login/index","/login/login","/login/login1","/login/out","/common/**","/pdf/**","/css/**","/js/**","/lib/**","/fonts/**","/img/**","/login/403","/drsoOriOrder/testBatchInsert/**");
registry.addInterceptor(rightsHandlerInterceptor()).addPathPatterns(allPowerList);
registry.addInterceptor(costTimeInterceptor()).addPathPatterns("/**").excludePathPatterns("/css/**","/js/**","/lib/**","/fonts/**","/img/**");
registry.addInterceptor(tokenHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/changeSessionLanauage?**","/analysis/**","/checkInterface/**","/error","/login/getVerify","/login/index","/login/login","/login/login1","/login/out","/common/**","/pdf/**","/css/**","/js/**","/lib/**","/fonts/**","/img/**","/login/403","/drsoOriOrder/testBatchInsert/**","/resources/i18n/**");
}
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
private List<String> addInterceptorRoad(){
allPowerList.add("/orgManage/deleteOrg");
//抽音管理
allPowerList.add("/drawSoundRule/init");
allPowerList.add("/drawSoundRule/editDrawSoundRule");
allPowerList.add("/drsoLog/init");
// 原始录音、订单
allPowerList.add("/recordings/init");
allPowerList.add("/drsoOriOrder/init");
// 质检模板
allPowerList.add("/qcTemplate/init");
// 初检任务
allPowerList.add("/qcTask/init");
allPowerList.add("/qcTask/toDetailPg");
// 复检任务
allPowerList.add("/qcSecTask/init");
allPowerList.add("/qcSecTask/toDetailPg");
// 流程管理
allPowerList.add("/manageProcess/init");
allPowerList.add("/manageProcess/add");
allPowerList.add("/manageProcess/edit");
//待复议
allPowerList.add("/reconsideration/init");
// 我的复议
allPowerList.add("/reconsideration/goMyReconsideration");
/* // 我的评分
allPowerList.add("/qcScoreHistory/init");*/
return allPowerList;
}
}
3.TokenHandlerInterceptor .java 拦截器
package com.hbsc.interceptor;
import com.alibaba.fastjson.JSON;
import com.hbsc.config.Token;
import com.hbsc.domain.IndexUserVo;
import com.hbsc.domain.common.ReturnMsg;
import com.hbsc.util.RedisUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.UUID;
/**
* token拦截器
*/
public class TokenHandlerInterceptor implements HandlerInterceptor {
private static final Logger logger = LoggerFactory.getLogger(TokenHandlerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.debug(">>>TokenHandlerInterceptor>>>>>>>QC-CHECK");
// 获取要访问的URL地址
IndexUserVo indexUserVo = (IndexUserVo) request.getSession().getAttribute("user");
String url = request.getRequestURL().toString();
String path=url.substring(url.lastIndexOf("/")+1);
if(handler instanceof HandlerMethod){
HandlerMethod handlerMethod=(HandlerMethod)handler;
Method method=handlerMethod.getMethod();
Token annotation=method.getAnnotation(Token.class);
if(annotation !=null){
boolean saveSes