我与 JSF 和 Spring Security 合作。我使用自定义登录页面。我有两个角色:管理员和用户。我的问题是如何针对不同的角色重定向到不同的页面。例如,如果用户是管理员,他将被重定向到“dashboard_Admin.jsf”,如果他是简单用户,他将被重定向到“dashboard_user.jsf”。
这是我的弹簧安全文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="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.1.xsd">
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/pages/**"
access="hasRole('ROLE_ADMIN')" />
<security:form-login login-page="/login.jsf"
authentication-failure-url="/login.jsf?error=true"
default-target-url="/pages/admin/dashboard_Admin.jsf" />
<security:logout logout-success-url="/login.jsf"
delete-cookies="JSESSIONID" invalidate-session="true" />
<security:session-management
invalid-session-url="/login.jsf">
<security:concurrency-control
max-sessions="1" error-if-maximum-exceeded="true" />
</security:session-management>
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider>
<security:user-service>
<security:user name="test" password="test"
authorities="ROLE_USER" />
<security:user name="sam" password="sam" authorities="ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
这是我的 doLogin 方法:
public String doLogin() throws ServletException, IOException {
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
.getRequestDispatcher("/j_spring_security_check?j_username=" + username
+ "&j_password=" + password);
dispatcher.forward((ServletRequest) context.getRequest(),
(ServletResponse) context.getResponse());
FacesContext.getCurrentInstance().responseComplete();
return null;
}