0

I'm just starting down the track of developing web apps and have started with JSF 2.2 on Java EE 7, GlassFish 4.

I thought I'd start with the very basics. I just want to protect the entire site, so every page you navigate to would first require you to authenticate yourself. So I read through the Java EE 7 Tutorial and tried the samples, modified them and then started to break them in ways I didn't think they would break. I tried all sorts of angles, but I'd generally end up in two situations:

  1. I'd try to access a page using a partial request, which would land me at /index.xhtml as defined by <welcome-file-list>, and be prompted to login; but on submitting the username/password I'd be instantly directed back to the login form.
  2. I added an action to the <h:commandButton> to point to index. This worked, but when I submitted the form on the index page which should take me to the response.xhtml page, I'd end up back at the login form instead of at the response page.

After many hours of trawling the net, it seemed that the reason I had broken the login procedure was because I had changed the plain HTML login form to use JSF tags like <h:form>, instead of <form>.

There is a discussion here that says you should not do this with login forms. To quote an interesting line from that page:

To make such a page login, make the actual login form be HTML and not JSF and code it according to the j2EE standards for login forms. Use the HTML form tag instead of the JSF form, and make sure you code an HTML SUBMIT button and not a JSF commandButton!

Once I changed it back to plain old HTML it worked. But I don't understand why. Can anyone enlighten me?! I think I am missing something fundamental which I need to understand if I'm going to start writing web apps in JSF.

Many thanks...

4

1 回答 1

2

这是因为<h:form>提交到当前 URL(在 Web 开发术语中也称为“回发”),而不是j_security_check,而基于表单的身份验证截获j_security_checkURL。

然而,使用 JSF 表单进行基于表单的身份验证是不可能的,这不是真的。很有可能,您只需要使用HttpServletRequest#login()自己在 backing bean 中执行登录即可。

也可以看看:

于 2013-07-26T02:48:41.030 回答