compile "com.fasterxml.jackson.core:jackson-databind:2.8.10"
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Login { String value() default ""; }
public class MemberDetails { private String memberId; private String memberName; private String memberNickname; private String memberPhone; private String memberEmail; }
/** * @Author: XiongFeng * @Description: 会员接口 * @Date: Created in 19:40 2018/4/10 */ public interface MemberService { /** 根据TokenId获取用户信息 */ MemberDto getMemberByToken(String token); }
@Service public class MemberServiceImpl implements MemberService { @Override public MemberDetails getMemberDetailsByToken(String token) { if (StringUtils.isBlank(token)) return null; if (!"123".equals(token)) return null; MemberDetails memberDetails = new MemberDetails(); memberDetails.setMemberId("123"); memberDetails.setMemberName("哈哈123"); memberDetails.setMemberEmail("seifon@seifon.cn"); memberDetails.setMemberNickname("Seifon"); memberDetails.setMemberPhone("13100001111"); return memberDetails; } }
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Vector; /** * @Author: XiongFeng * @Description: 对Request请求重新包装 * @Date: Created in 11:17 2018/4/13 */ public class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map<String , String[]> params = new HashMap<String, String[]>(); @SuppressWarnings("unchecked") public ParameterRequestWrapper(HttpServletRequest request) { // 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似 super(request); //将参数表,赋予给当前的Map以便于持有request中的参数 this.params.putAll(request.getParameterMap()); } //重载一个构造方法 public ParameterRequestWrapper(HttpServletRequest request , Map<String , Object> extendParams) { this(request); addAllParameters(extendParams);//这里将扩展参数写入参数表 } /** * 复写获取key的方法 */ @Override public Enumeration getParameterNames() { Vector names = new Vector(params.keySet()); return names.elements(); } /** * 复写获取值value的方法 */ @Override public String getParameter(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { String[] strArr = (String[]) v; if (strArr.length > 0) { return strArr[0]; } else { return null; } } else if (v instanceof String) { return (String) v; } else { return v.toString(); } } @Override public String[] getParameterValues(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { return (String[]) v; } else if (v instanceof String) { return new String[] { (String) v }; } else { return new String[] { v.toString() }; } } public void addAllParameters(Map<String , Object>otherParams) {//增加多个参数 for(Map.Entry<String , Object>entry : otherParams.entrySet()) { addParameter(entry.getKey() , entry.getValue()); } } public void addParameter(String name , Object value) {//增加参数 if(value != null) { if(value instanceof String[]) { params.put(name , (String[])value); }else if(value instanceof String) { params.put(name , new String[] {(String)value}); }else { params.put(name , new String[] {String.valueOf(value)}); } } } }
import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @Author: XiongFeng * @Description: 会员登录信息过滤器 * @Date: Created in 11:17 2018/4/13 */ @Component @WebFilter(urlPatterns = "/*") public class MemberFilter implements Filter { MemberService memberService = new MemberServiceImpl(); ObjectMapper objectMapper = new ObjectMapper(); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String tokenId = req.getHeader("X-Authorization"); if (tokenId == null || "".equals(tokenId) || tokenId.isEmpty()) { chain.doFilter(request, response); return; } MemberDetails memberDetails = memberService.getMemberDetailsByToken(tokenId); if (memberDetails == null) { try { respFail(response); return; } catch (Exception e) { throw new ServletException(); } } Map<String, String[]> parameterMap = new HashMap<String, String[]>(request.getParameterMap()); parameterMap.put("memberId", new String[]{memberDetails.getMemberId()}); parameterMap.put("memberName", new String[]{memberDetails.getMemberName()}); parameterMap.put("memberEmail", new String[]{memberDetails.getMemberEmail()}); parameterMap.put("memberNickname", new String[]{memberDetails.getMemberNickname()}); parameterMap.put("memberPhone", new String[]{memberDetails.getMemberPhone()}); chain.doFilter(new ParameterRequestWrapper((HttpServletRequest) req, parameterMap), response); } /** 返回失败结果Json数据 */ private void respFail(ServletResponse response) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("status", 500); map.put("message", "登录失效,请登录"); map.put("data", null); String s = objectMapper.writeValueAsString(map); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); response.getWriter().write(s); } @Override public void destroy() { } }
package cn.seifon.paymodle.interceptor; import cn.seifon.paymodle.annotations.Login; import cn.seifon.paymodle.dto.MemberDetails; import cn.seifon.paymodle.service.manager.member.MemberService; import cn.seifon.paymodle.service.manager.member.impl.MemberServiceImpl; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.MethodParameter; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; /** * @Author: XiongFeng * @Description: 会员登录信息拦截器 * @Date: Created in 11:17 2018/4/13 */ public class MemberInterceptor extends HandlerInterceptorAdapter { ObjectMapper objectMapper = new ObjectMapper(); MemberService memberService = new MemberServiceImpl(); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod method = (HandlerMethod) handler; String[] memberIds = request.getParameterValues("memberId"); MethodParameter[] methodParameters = method.getMethodParameters(); //判断方法类是否有MemberDetails入参 if (methodParameters.length > 0) { for (MethodParameter methodParameter : methodParameters) { Type genericParameterType = methodParameter.getGenericParameterType(); String typeName = genericParameterType.getTypeName(); if (!typeName.equals(MemberDetails.class.getTypeName())) continue; if (memberIds == null || memberIds.length <= 0) return this.respFail(response); //如果找不到用户信息就返回失败 break; } } //判断是否有Login注解 Login login = method.getMethodAnnotation(Login.class); if (login == null) return true; if (memberIds == null || memberIds.length <= 0) return this.respFail(response); //如果找不到用户信息就返回失败 return true; } /** 返回失败结果Json数据 */ private boolean respFail(HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("status", 500); map.put("message", "登录失效,请登录"); map.put("data", null); String s = objectMapper.writeValueAsString(map); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); response.getWriter().write(s); return false; } }
@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns 用于添加拦截规则 registry.addInterceptor(new MemberInterceptor()).addPathPatterns("/**"); super.addInterceptors(registry); } }
@RestController public class MemberController { @RequestMapping("/token") @Login public Map<String, Object> getUser(MemberDetails memberDetails) { //User user = userManager.selectByPrimaryKey(id); Map<String, Object> map = new LinkedHashMap<>(); map.put("status", 200); map.put("message", "请求成功"); map.put("data", memberDetails); return map; } }
运行结果: