配置基本环境
配置基本环境:spring-security基本环境配置
项目目录结构如下图所示:
实现UsernamePasswordAuthenticationFilter子类
public class MUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
for (Object key : req.getParameterMap().keySet()) {
System.out.println(key + ", " + req.getParameter(key.toString()));
}
super.doFilter(req, res, chain);
}
}
修改security.xml文件
<http pattern="/login.jsp" security="none" />
<http use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<csrf disabled="true" />
<custom-filter ref="captchaAuthenticaionFilter"
before="FORM_LOGIN_FILTER" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="user" password="password" authorities="ROLE_USER" />
<user name="admin" password="admin" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
<b:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<b:constructor-arg index="0" value="/login.jsp" />
</b:bean>
<b:bean id="captchaAuthenticaionFilter" class="com.sfq.MUsernamePasswordAuthenticationFilter">
<b:property name="authenticationManager" ref="authenticationManager" />
<b:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler" />
<b:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler" />
<b:property name="filterProcessesUrl" value="/j_spring_security_check" />
</b:bean>
<b:bean id="loginLogAuthenticationSuccessHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<b:property name="defaultTargetUrl" value="/index.jsp" />
</b:bean>
<b:bean id="simpleUrlAuthenticationFailureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<b:property name="defaultFailureUrl" value="/login.jsp" />
</b:bean>
网友评论