forked from chenxuebing3/learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
使用过滤器对请求进行权限校验
- Loading branch information
Showing
9 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
WebSocket/ws-push/src/main/java/com/zhangln/push/wspush/config/prop/AppProp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.zhangln.push.wspush.config.prop; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author sherry | ||
* @description | ||
* @date Create in 2019/12/9 | ||
* @modified By: | ||
*/ | ||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "user") | ||
public class AppProp { | ||
|
||
private String active; | ||
private List<String> ignoreUrl; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
WebSocket/ws-push/src/main/java/com/zhangln/push/wspush/filter/TimeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.zhangln.push.wspush.filter; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.annotation.WebFilter; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
/** | ||
* 时间过滤器 | ||
*/ | ||
@WebFilter("/**") | ||
@Component | ||
@Slf4j | ||
public class TimeFilter implements Filter { | ||
|
||
@Override | ||
public void destroy() { | ||
log.info("time filter destroy"); | ||
} | ||
|
||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
log.info("time filter start"); | ||
long start = new Date().getTime(); | ||
chain.doFilter(request, response); | ||
log.info("time filter 耗时:"+ (new Date().getTime() - start)+"毫秒"); | ||
log.info("time filter finish"); | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig arg0) throws ServletException { | ||
log.info("time filter init"); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
WebSocket/ws-push/src/main/java/com/zhangln/push/wspush/filter/UrlFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.zhangln.push.wspush.filter; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.zhangln.push.wspush.config.prop.AppProp; | ||
import com.zhangln.push.wspush.controller.service.AccessService; | ||
import com.zhangln.push.wspush.vo.HttpResVo; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.AntPathMatcher; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.annotation.WebFilter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author sherry | ||
* @description | ||
* @date Create in 2019/12/9 | ||
* @modified By: | ||
*/ | ||
@WebFilter("/**") | ||
@Component | ||
@Slf4j | ||
public class UrlFilter implements Filter { | ||
|
||
@Autowired | ||
private AppProp appProp; | ||
|
||
@Autowired | ||
private AntPathMatcher antPathMatcher; | ||
|
||
@Autowired | ||
private AccessService accessService; | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; | ||
String url = httpRequest.getRequestURI(); | ||
log.info("当前请求地址:{}", url); | ||
// 可以被忽略的地址 | ||
List<String> urls = appProp.getIgnoreUrl(); | ||
|
||
for (String tmp : urls) { | ||
boolean flag = antPathMatcher.match(tmp, url); | ||
if (flag) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} | ||
} | ||
|
||
// 执行token认证 | ||
|
||
String token = httpRequest.getHeader("access"); | ||
boolean exists = accessService.exists(token); | ||
|
||
if (exists) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} else { | ||
// 校验不通过 | ||
HttpServletResponse response = (HttpServletResponse) servletResponse; | ||
response.setHeader("content-type", "application/json;charset=utf-8"); | ||
log.info("token校验不通过"); | ||
response.getWriter().write(JSONObject.toJSONString(HttpResVo.buildError("token无效:"+token))); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters