spring security添加自定义filter

时间:2021-03-12 21:40:30

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<beans:import resource="/spring-database.xml"/>
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/index.jsp" access="hasRole('ADMIN')"/>
<form-login login-page="/login.jsp" login-processing-url="/check_action"
username-parameter="username"
password-parameter="password"/>
<session-management>
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</session-management>
<custom-filter ref="myfilter" position="FIRST"/>
</http>
<beans:bean id="myfilter" class="com.wyc.filter.MyFilter"></beans:bean>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username username,password password,enabled enabled from _user where username=?"
authorities-by-username-query="select username username,role role from _user_role where username=?"/>
</authentication-provider>
</authentication-manager>
</beans:beans>


其中,重点语句是

<custom-filter ref="myfilter" position="FIRST"/>

以及

<beans:bean id="myfilter" class="com.wyc.filter.MyFilter"></beans:bean>


MyFilter.java

package com.wyc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter{

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("myFilter");
arg2.doFilter(arg0, arg1);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}


附:其他配置文件以及maven库依赖参见下面文章:

http://blog.csdn.net/wyccyw123456/article/details/51913054