gpt4 book ai didi

java - 将行添加到安全配置后出现 ERR_TOO_MANY_REDIRECTS

转载 作者:行者123 更新时间:2023-11-29 10:15:34 25 4
gpt4 key购买 nike

我添加行

<intercept-url pattern="/*" access="isAuthenticated()"/> 

到 security_config.xml浏览器说我

ERR_TOO_MANY_REDIRECTS

安全配置.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http use-expressions="true">
<!-- <intercept-url pattern="/*" access="permitAll" /> -->
<intercept-url pattern="/*" access="isAuthenticated()"/>
<form-login login-page="/home.jsp"
authentication-failure-url="/loginFailed" default-target-url="/index" />
<logout logout-success-url="/logOut" />
</http>
<authentication-manager>
<!-- <authentication-provider ref="provider" /> -->
<authentication-provider>
<user-service>
<user name="name" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>

</beans:beans>

主页.jsp:

<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello,
<sec:authentication property="principal" />!
</h1>
<c:set var="username">
<sec:authentication property="principal" />
</c:set>
<p style="color:#ff0000">${message}</p>

<c:if test="${username != 'anonymousUser'}">
<form method="POST" action="j_spring_security_logout">
<input type="submit" value="log out">
</form>
<jsp:include page="WEB-INF/views/menu.jsp" flush="true" />
</c:if>
<form method="POST" action="<c:url value="/j_spring_security_check" />" <c:if test="${username != 'anonymousUser'}">hidden="true"</c:if>>
<table>
<tr>
<td align="right">login</td>
<td><input type="text" name="j_username" id="login"
onkeyup="validate()" /></td>
</tr>
<tr>
<td align="right">password</td>
<td><input type="password" name="j_password" id ="passwordId" onkeyup="validate()" /></td>
</tr>
<tr>
<td align="right">remember me</td>
<td><input type="checkbox" name="_spring_security_remember_me" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit"
value="Login" id="idSubmit" disabled /> <input type="reset"
value="Reset" /></td>
</tr>
</table>
</form>

</body>
<script type="text/javascript">
function validate() {
element = document.getElementById("idSubmit");
element1 = document.getElementById("login");
resultMatch = element1.value.match('([a-zA-Z0-9])+(_){1}([a-zA-Z0-9])+')
if (resultMatch == null){
element.setAttribute("disabled", "disabled");
return
}
if(resultMatch[0] == element1.value && document.getElementById("passwordId").value !="" ){
element.removeAttribute("disabled");
return
}
else
element.setAttribute("disabled", "disabled");

}
window.onload = "validate()";
</script>
</html>

但如果我这样写

<intercept-url pattern="/*" access="permitAll" /> 

效果不错。

你能帮帮我吗?

最佳答案

<intercept-url pattern="/*" access="isAuthenticated()"/>

表示所有 URL 都需要身份验证。这包括您的登录 URL。发生的事情是你点击了一个 URL,spring 发现需要身份验证,所以它重定向到登录 URL,但是除非你被授权,否则你无法访问登录 URL,所以它会将你重定向到登录 URL - 因此无限重定向循环。

Spring 按照您定义的顺序评估拦截 URL,因此您可以通过添加一行 above 来解决这个问题,告诉 spring 不需要 auth用于登录 URL。您还应该为注销和登录失败后转发到的 URL 添加一行,否则它只会要求您重新登录。

<intercept-url pattern="/home.jsp" access="permitAll" /> 
<intercept-url pattern="/*" access="isAuthenticated()" />

关于java - 将行添加到安全配置后出现 ERR_TOO_MANY_REDIRECTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com