【问题标题】:Is it possible to have URL based authentication with Form based authentication?是否可以通过基于表单的身份验证进行基于 URL 的身份验证?
【发布时间】:2017-07-02 18:35:27
【问题描述】:

登录页面

<form method='post' action='j_security_check'>
	<input type='text' name='j_username'>
	<input type='password' name='j_password'>
</form>

来自 web.xml 的内容

<web-app>
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>User Auth</web-resource-name>
			<url-pattern>/auth/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>admin</role-name>
			<role-name>manager</role-name>
		</auth-constraint>
	</security-constraint>
	<login-config>
		<auth-method>FORM</auth-method>
		<form-login-config>
			<form-login-page>login.jsp</form-login-page>
			<form-error-page>error.jsp</form-error-page>
		</form-login-config>
	</login-config>
</web-app>

基于表单的身份验证工作正常。但是,如果我直接从http://localhost:8080/index.jsp/j_security_check?j_username=admin&j_password=admin 访问 index.jsp 将无法正常工作。

为什么它不起作用?

【问题讨论】:

  • method='post' 但是浏览器发送了一个 GET 请求(据我所知),如果你真的想这样做,你也需要管理 GET 请求。 (但真的有必要吗?如果你真的需要,请使用重定向页面)
  • 当您在表单中使用 method='post' 时,请求将作为帖子处理 - 但是,当您只是按照您的指示直接传递查询参数时 -请求将作为 get(即 != post)处理,因此不起作用

标签: java tomcat


【解决方案1】:

由于您希望通过 POST 请求登录,因此您不能(如果我错了,请纠正我)直接从浏览器 URL 发送。

一个解决方案是创建另一个页面,loginRedirect.jsp,它会自动发送一个 POST 请求。

这将使用 URL 中传递的参数填充 &lt;form&gt; 并自动发送 click 事件。一切都在 Javascript 中。

这可以在login 页面中完成。如果 URL 中有参数,则自动发送表单。

使用此answer 作为源,您可以轻松读取参数(并检查是否有)并设置&lt;input&gt; 的值。然后点击submit按钮。

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 2014-12-13
    • 2021-01-25
    • 2017-09-18
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2013-01-20
    相关资源
    最近更新 更多