失效。
以下是成功配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:s="http://www.springframework.org/schema/security" 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/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"
- default-lazy-init="true">
- <description>SpringSecurity安全配置</description>
- <!-- http安全配置 -->
- <s:http auto-config="true" use-expressions="true">
- <s:intercept-url pattern="/css/**" filters="none" />
- <s:intercept-url pattern="/img/**" filters="none" />
- <s:intercept-url pattern="/js/**" filters="none" />
- <s:intercept-url pattern="/index.jsp" filters="none" />
- <s:intercept-url pattern="/login.action"
- access="hasAnyRole('ROLE_ANONYMOUS')" />
- <s:intercept-url pattern="/logout.jsp"
- access="hasAnyRole('ROLE_ANONYMOUS')" />
- <s:intercept-url pattern="/main/**" access="hasAnyRole('ROLE_通用')" />
- <s:intercept-url pattern="/public*"
- access="hasAnyRole('ROLE_ANONYMOUS')" />
- <s:intercept-url pattern="/public/test.action"
- access="hasAnyRole('ROLE_ANONYMOUS')" />
- <s:intercept-url pattern="/**" access="isAuthenticated()" />
- <s:intercept-url pattern="/account/user!setSession*"
- access="hasAnyRole('ROLE_通用')" />
- <s:intercept-url pattern="/account/user!changepwd*"
- access="hasAnyRole('ROLE_通用')" />
- <s:intercept-url pattern="/account/user!save*" access="hasAnyRole('ROLE_修改用户')" />
- <s:intercept-url pattern="/account/user!delete*"
- access="hasAnyRole('ROLE_修改用户')" />
- <s:intercept-url pattern="/account/user*" access="hasAnyRole('ROLE_浏览用户')" />
- <s:intercept-url pattern="/account/role!save*" access="hasAnyRole('ROLE_修改角色')" />
- <s:intercept-url pattern="/account/role!delete*"
- access="hasAnyRole('ROLE_修改角色')" />
- <s:intercept-url pattern="/account/role*" access="hasAnyRole('ROLE_浏览角色')" />
- <s:intercept-url pattern="/lab/lab!save*" access="hasAnyRole('ROLE_修改实验室')" />
- <s:intercept-url pattern="/lab/lab!delete*" access="hasAnyRole('ROLE_修改实验室')" />
- <s:intercept-url pattern="/lab/lab*" access="hasAnyRole('ROLE_浏览实验室')" />
- <s:form-login login-page="/login.action"
- default-target-url="/main.action" authentication-failure-url="/login.action?error=true" />
- <s:logout logout-success-url="/logout.jsp" />
- <s:custom-filter before="FORM_LOGIN_FILTER" ref="appSessionProcessingFilter" />
- </s:http>
- <!-- 自定义成功和失败处理器,AppSessionSuccessHandler中设置了session -->
- <bean id="appSessionProcessingFilter"
- class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
- <property name="authenticationFailureHandler">
- <bean
- class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
- <property name="defaultFailureUrl" value="/pages/Login/login.do?error=true" />
- </bean>
- </property>
- <property name="authenticationSuccessHandler">
- <bean class="mis.service.account.AppSessionSuccessHandler">
- <property name="defaultTargetUrl" value="/" />
- </bean>
- </property>
- <property name="authenticationManager" ref="authenticationManager"/>
- <property name="filterProcessesUrl" value="/j_spring_security_check"/>
- </bean>
- <!-- 认证配置, 使用userDetailsService提供的用户信息 -->
- <s:authentication-manager alias="authenticationManager">
- <s:authentication-provider user-service-ref="userDetailsService">
- <s:password-encoder hash="plaintext" />
- </s:authentication-provider>
- </s:authentication-manager>
- <!-- 项目实现的用户查询服务 -->
- <bean id="userDetailsService" class="mis.service.account.UserDetailsServiceImpl" />
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:s="http://www.springframework.org/schema/security" 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/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"
default-lazy-init="true">
<description>SpringSecurity安全配置</description>
<!-- http安全配置 -->
<s:http auto-config="true" use-expressions="true">
<s:intercept-url pattern="/css/**" filters="none" />
<s:intercept-url pattern="/img/**" filters="none" />
<s:intercept-url pattern="/js/**" filters="none" />
<s:intercept-url pattern="/index.jsp" filters="none" />
<s:intercept-url pattern="/login.action"
access="hasAnyRole('ROLE_ANONYMOUS')" />
<s:intercept-url pattern="/logout.jsp"
access="hasAnyRole('ROLE_ANONYMOUS')" />
<s:intercept-url pattern="/main/**" access="hasAnyRole('ROLE_通用')" />
<s:intercept-url pattern="/public*"
access="hasAnyRole('ROLE_ANONYMOUS')" />
<s:intercept-url pattern="/public/test.action"
access="hasAnyRole('ROLE_ANONYMOUS')" />
<s:intercept-url pattern="/**" access="isAuthenticated()" />
<s:intercept-url pattern="/account/user!setSession*"
access="hasAnyRole('ROLE_通用')" />
<s:intercept-url pattern="/account/user!changepwd*"
access="hasAnyRole('ROLE_通用')" />
<s:intercept-url pattern="/account/user!save*" access="hasAnyRole('ROLE_修改用户')" />
<s:intercept-url pattern="/account/user!delete*"
access="hasAnyRole('ROLE_修改用户')" />
<s:intercept-url pattern="/account/user*" access="hasAnyRole('ROLE_浏览用户')" />
<s:intercept-url pattern="/account/role!save*" access="hasAnyRole('ROLE_修改角色')" />
<s:intercept-url pattern="/account/role!delete*"
access="hasAnyRole('ROLE_修改角色')" />
<s:intercept-url pattern="/account/role*" access="hasAnyRole('ROLE_浏览角色')" />
<s:intercept-url pattern="/lab/lab!save*" access="hasAnyRole('ROLE_修改实验室')" />
<s:intercept-url pattern="/lab/lab!delete*" access="hasAnyRole('ROLE_修改实验室')" />
<s:intercept-url pattern="/lab/lab*" access="hasAnyRole('ROLE_浏览实验室')" />
<s:form-login login-page="/login.action"
default-target-url="/main.action" authentication-failure-url="/login.action?error=true" />
<s:logout logout-success-url="/logout.jsp" />
<s:custom-filter before="FORM_LOGIN_FILTER" ref="appSessionProcessingFilter" />
</s:http>
<!-- 自定义成功和失败处理器,AppSessionSuccessHandler中设置了session -->
<bean
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationFailureHandler">
<bean
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/pages/Login/login.do?error=true" />
</bean>
</property>
<property name="authenticationSuccessHandler">
<bean class="mis.service.account.AppSessionSuccessHandler">
<property name="defaultTargetUrl" value="/" />
</bean>
</property>
<property name="authenticationManager" ref="authenticationManager"/>
<property name="filterProcessesUrl" value="/j_spring_security_check"/>
</bean>
<!-- 认证配置, 使用userDetailsService提供的用户信息 -->
<s:authentication-manager alias="authenticationManager">
<s:authentication-provider user-service-ref="userDetailsService">
<s:password-encoder hash="plaintext" />
</s:authentication-provider>
</s:authentication-manager>
<!-- 项目实现的用户查询服务 -->
<bean />
</beans>
- package mis.service.account;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import mis.dao.account.UserDao;
- import mis.entity.account.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
- public class AppSessionSuccessHandler extends
- SavedRequestAwareAuthenticationSuccessHandler {
- @Autowired
- private UserDao userDao;
- @Override
- public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication)throws ServletException,IOException{
- HttpSession session=request.getSession();
- UserDetails userDetails = (UserDetails) authentication.getPrincipal();
- User currentUser = userDao.findUnique("loginname", userDetails.getUsername().toString());
- session.setAttribute("currentUser", currentUser);
- System.out.println("do it success");
- super.onAuthenticationSuccess(request,response,authentication);}
- public UserDao getUserDao() {
- return userDao;
- }
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }}