如何复写AuthenticationFilter继承AbstractCasFilter不拦截某些请求
时间: 2023-10-27 20:29:54 浏览: 44
好的,针对您的问题,您可以继承 `AbstractCasFilter` 类,然后重写 `doFilter` 方法,并在该方法中判断当前请求的 URL 是否是需要跳过 CAS 认证的 URL。如果是,直接调用 `filterChain.doFilter(request, response)`,则该请求将不会经过 CAS 认证过滤器,否则调用 `super.doFilter(request, response, filterChain)`,则该请求将继续经过 CAS 认证过滤器。具体实现可以参考下面的代码:
```java
public class CustomCasFilter extends AbstractCasFilter {
private List<String> excludeUrls;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String requestURI = request.getRequestURI();
// 判断当前请求的 URL 是否是需要跳过 CAS 认证的 URL
for (String excludeUrl : excludeUrls) {
if (requestURI.matches(excludeUrl)) {
filterChain.doFilter(request, response);
return;
}
}
super.doFilter(request, response, filterChain);
}
public void setExcludeUrls(List<String> excludeUrls) {
this.excludeUrls = excludeUrls;
}
}
```
其中,`excludeUrls` 是一个列表,里面存放需要跳过 CAS 认证的 URL,可以通过 Spring 的配置文件进行配置,例如:
```xml
<bean id="casFilter" class="com.xxx.CustomCasFilter">
<property name="excludeUrls">
<list>
<value>/api/**</value>
<value>/login</value>
</list>
</property>
<property name="casServerLoginUrl" value="${cas.server.login.url}"/>
<property name="serverName" value="${server.name}"/>
<property name="redirectAfterValidation" value="${cas.redirect.after.validation}"/>
<property name="useSession" value="${cas.use.session}"/>
</bean>
```
以上是一个简单的实现思路,您可以根据实际情况进行修改。希望对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)