springboot 過濾器 -程式碼
阿新 • • 發佈:2019-01-12
總的過濾器,設定過濾器順序
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; /** * @author wangwei * @version v1.0.0 * @description 總過濾器 * @date 2019-10-12 */ @Configuration public class FilterConfig { @Bean public FilterRegistrationBean buildBFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setOrder(2); filterRegistrationBean.setFilter(new SignFilter()); filterRegistrationBean.setName("SignFilter"); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } @Bean public FilterRegistrationBean buildCFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setOrder(3); filterRegistrationBean.setFilter(new LoginValidateFilter()); filterRegistrationBean.setName("LoginValidateFilter"); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } }
分過濾器
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; /** * @author wangwei * @version v1.0.0 * @description 簽名過濾器, 簽名不攔截open下的介面 * @date 2019-01-12 */ @Component @WebFilter(filterName="SignFilter", urlPatterns="/*") @RefreshScope public class SignFilter implements Filter { @Value("${version}") private String version; @Override public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String path = request.getRequestURI(); if(path.indexOf("/api/open/")> -1 ){ filterChain.doFilter(servletRequest, servletResponse); return; } Map<String, String> map = SignUtil.toVerifyMap(request.getParameterMap(),false); String secretKey = map.get("secretKey"); if (StringUtils.isEmpty(secretKey) || !map.get("secretKey").equals(SignUtil.getInstance().secretkey)){ System.out.println("secretKey is err"); PrintWriter writer = null; servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html; charset=utf-8"); try { writer = servletResponse.getWriter(); String userJson = "{\"code\":\" "+ ErrorCode.CODE_431.getCode() +"\", \"message\": \""+ ErrorCode.CODE_431.getMessage() +"\"}"; writer.print(userJson); } catch (IOException e1) { } finally { if (writer != null) writer.close(); } } if (SignUtil.getInstance().verify(map)){ // 簽名成功 filterChain.doFilter(servletRequest, servletResponse); return; }else { PrintWriter writer = null; servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html; charset=utf-8"); try { writer = servletResponse.getWriter(); String userJson = "{\"code\":\" "+ ErrorCode.CODE_430.getCode() +"\", \"message\": \""+ ErrorCode.CODE_430.getMessage() +"\"}"; writer.print(userJson); } catch (IOException e1) { } finally { if (writer != null) writer.close(); } } } @Override public void destroy() { } }
分過濾器2
import com.alibaba.druid.support.json.JSONUtils; import io.jsonwebtoken.Claims; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; /** * @author wangwei * @version v1.0.0 * @description 登入驗證過濾器Filter * @date 2019-01-10 */ @Component @WebFilter(filterName="LoginValidateFilter", urlPatterns="/*") @RefreshScope public class LoginValidateFilter implements Filter { @Value("${version}") private String version; @Override public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { String token = ((HttpServletResponse) servletResponse).getHeader("token"); if(StringUtils.isBlank(token)) { token = servletRequest.getParameter("token"); } HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String path = request.getRequestURI(); if(path.indexOf("/api/open/")> -1 ){ filterChain.doFilter(servletRequest, servletResponse); return; } if(StringUtils.isNotBlank(token)) { // 判斷是否過期 沒有過期在header更新token try{ Claims claims = JWTUtils.parseJWT(token); ((HttpServletResponse) servletResponse).setHeader("token", JWTUtils.createJWT(claims.getId(), claims.getIssuer(), claims.getSubject(), 1000 * 60 * 30)); filterChain.doFilter(servletRequest, servletResponse); } catch (Exception e) { PrintWriter writer = null; servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html; charset=utf-8"); try { writer = servletResponse.getWriter(); String userJson = "{\"code\":\" "+ ErrorCode.UNLOGIN_ERROR.getCode() +"\", \"message\": \""+ ErrorCode.UNLOGIN_ERROR.getMessage() +"\"}"; writer.print(userJson); } catch (IOException e1) { } finally { if (writer != null) writer.close(); } } } else { PrintWriter writer = null; servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html; charset=utf-8"); try { writer = servletResponse.getWriter(); String userJson = "{\"code\":\" "+ ErrorCode.PARAMETER_CHECK_ERROR.getCode() +"\", \"message\": \""+ ErrorCode.PARAMETER_CHECK_ERROR.getMessage() +"\"}"; writer.print(userJson); } catch (IOException e1) { } finally { if (writer != null) writer.close(); } } } @Override public void destroy() { } }