设置:
- 玻璃鱼 3.1.1
- JSF 2.1
- 带有 postgreSQL 的 jdbcRealm
在登录表单中输入有效的用户凭据后,我登录并重定向到下一页。但是,如果我离开该页面并转到另一个页面,当然这是在允许的 url 模式中,我会得到“HTTP 状态 403 - 对所请求资源的访问已被拒绝”。之后,我无法再访问 webapp 的任何站点。
只有在 glassfish 的 server-config/security 中将 Standard-Realm 设置为我自己的领域时,登录才有效!
我在部署时在服务器日志中收到一条警告:
Warnung: Keine Principals zugeordnet zu Rolle [USER].
(Warning: No principals mapped to role [USER])
如果我使用 Chrome 调试器删除 JSESSIONID,我可以再次登录。看起来会话在访问一个允许的网站或其他东西后在服务器端被破坏?!
我附上了一些相关的资料。我认为它与Realm等无关,因为登录机制有效并且没有例外......
web.xml
<login-config>
<auth-method>FORM</auth-method>
<realm-name>jdbcRealm</realm-name>
<form-login-config>
<form-login-page>/faces/login.xhtml</form-login-page>
<form-error-page>/faces/loginError.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>User</web-resource-name>
<url-pattern>/faces/user/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
glassfish-web-app.xml(手动添加)
<glassfish-web-app>
<security-role-mapping>
<role-name>USER</role-name>
<group-name>USER</group-name>
</security-role-mapping>
</glassfish-web-app>
登录.xhtml
<h:form>
<h:outputLabel for="usernameInput">
Username:
</h:outputLabel>
<h:inputText id="usernameInput" value="#{authBackingBean.username}"
required="true" />
<br />
<h:outputLabel for="passwordInput">
Password:
</h:outputLabel>
<h:inputSecret id="passwordInput" value="#{authBackingBean.password}"
required="true" />
<br />
<h:commandButton value="Login"
action="#{authBackingBean.login}" />
AuthBackingBean
@Stateless
@Named
public class AuthBackingBean {
private String username;
private String password;
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
request.login(this.username, this.password);
} catch (ServletException e) {
context.addMessage(null, new FacesMessage("Login failed."));
return "loginError";
}
return "user/index";
}
public void logout() {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
request.logout();
} catch (ServletException e) {
context.addMessage(null, new FacesMessage("Logout failed."));
}
}
}
PS:我对 oracle 的文件非常失望,因为它包含许多逻辑错误、拼写错误和复制/粘贴错误。它是非结构化的、难以阅读和超载的。(对不起,但不得不说)