根据Spring中的用户角色映射欢迎文件
我的Web应用程序有两个角色,用户和管理员,每个角色都有不同的jsp页面。现在,我如何为这两个不同的用户映射不同的欢迎文件? 用于用户的Server/webapp/user.jsp 用于admin的server/webpp/admin.jsp根据Spring中的用户角色映射欢迎文件,spring,jsp,spring-mvc,spring-security,Spring,Jsp,Spring Mvc,Spring Security,我的Web应用程序有两个角色,用户和管理员,每个角色都有不同的jsp页面。现在,我如何为这两个不同的用户映射不同的欢迎文件? 用于用户的Server/webapp/user.jsp 用于admin的server/webpp/admin.jsp 比方说,如果将欢迎文件添加为user.jsp/user home page。如何在user.jsp中为管理员用户添加条件?我觉得Cookie或过滤器会有所帮助。您可以在控制器中处理它: @RequestMapping(value="/loginUs
比方说,如果将欢迎文件添加为user.jsp/user home page。如何在user.jsp中为管理员用户添加条件?我觉得Cookie或过滤器会有所帮助。您可以在控制器中处理它:
@RequestMapping(value="/loginUser")
public String controllerLogin(){
String userType = //code to get user type
//code to get view depending on the userType (like a switch)
return "view";
}
如果返回不带@RequestBody的字符串,它将被视为视图名称。
这是来自Spring3.X的,我想这就是您正在使用的
变体1。您可以使用注释并为每个角色创建不同的文件。
例如,您的管理员有role“role\u Admin”
只需添加以下内容:
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping("/login")
public String getAdmin() {
return "admin";
}
用户也一样:
@PreAuthorize("hasRole('ROLE_USER')")
@RequestMapping("/login")
public String getUser() {
return "user";
}
变体2。如果您希望根据角色在.jsp文件中执行特殊操作,请使用以下命令:
<sec:authorize access="hasRole('ROLE_ADMIN')">
This content will only be visible to users who have
the "ROLE_ADMIN" authority in their list of <tt>GrantedAuthority</tt>s.
</sec:authorize>
此内容仅对具有以下权限的用户可见:
他们的授权权限列表中的“角色\管理”权限。
要使此标记正常工作,请在.jsp文件的开头添加此标记:
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
更多关于
authorize
tag的信息,谢谢。假设我将欢迎文件添加为user.jsp/user主页。如何在user.jsp中为管理员用户添加条件?好吧,您可以使用JSTL,看看它是否是第一页(索引页),您将如何确定用户的角色?如果是仪表板/主页,@Optio的解决方案有效