2
我正在实现我自己的身份验证机制,我想知道我所做的是否正确,如果不正确,我该如何做。如何限制未登录的用户访问某些页面? (JSF 2.0)
首先生病解释一下我的身份验证机制是如何工作的:
我的用户-The细节都称为角色的对象内。这个对象包含3个字段:
电子邮件:String
密码:String
USERTYPE:Enum
- 当用户访问系统,该对象的作用是保存到会话中。
我的问题是:我如何限制基于userType
字段的用户(角色)访问某些页面?
这是我做的,但没有工作。
首先我有一个托管bean来检查usser是否被记录。
@ManagedBean
@RequestScoped
public class SecurityController {
//Some attributes...
public String redirectNotBuyer() {
Role role = (Role) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("userRole");
//Checks if user is logged
if (role == null) {
// Please login
//Add message to authentification
return "login.xhtml";
} else if (role != null) {
if (!role.getType().toString().equalsIgnoreCase("BUYER")) {
// Buyer not authorized
return "main.xhtml";
}
}
return null;
}
public String redirectNotSeller() {
Role role = (Role) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("userRole");
if (role == null) {
// Please login
//Add message to authentification
return "login.xhtml";
} else if (role != null) {
if (!role.getType().toString().equalsIgnoreCase("SELLERs")) {
// Buyer not authorized
return "main.xhtml";
}
}
return null;
}
//Getters, setters...
上述2种方法在用户不是买方的情况下重定向并且用户不是卖方的情况下。
所以,现在我所做的是在页面中,我不希望用户去我打电话其中一种方法,所以用户被重定向到主页面。 例:一个非授权的用户进入一个页面被称为buyOffer.xhtml,只有买家可以访问:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<!-- THE REGISTRATION FORM -->
<ui:define name="buyOfferForm">
<h2>Buy offer</h2>
#{SecurityController.redirectNotBuyer()}
</ui:define>
</ui:composition>
</html>
出于某种原因,当我去到这个网页与用户没有登录,或者是用户没有BUYER作为userType,它不会被重定向到main.xhtml页面。这是为什么?