我第一次尝试配置我的spring-security项目但是当我尝试登录时收到一条消息:" ERR_TOO_MANY_REDIRECTS"。可能在某个地方,它会再次重定向到管理员,但在哪里? 这是我的spring-security.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<http use-expressions="true" >
<csrf disabled="true"/>
<intercept-url pattern="/admin" access="hasRole('Admin')" />
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/logout" access="permitAll" />
<access-denied-handler error-page="/403" />
<form-login login-page='/login' login-processing-url="/login" authentication-failure-url="/login?error=true"
username-parameter="username" password-parameter="password" />
<logout logout-url="/logout" logout-success-url="/logoutSuccessful" delete-cookies="JSESSIONID" invalidate-session="true" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="myDataSource"
users-by-username-query= "select login, password, 'true' from employee where login=?"
authorities-by-username-query= "select login, role from employee where login =? " />
</authentication-provider>
</authentication-manager>
<beans:import resource="data-source-cfg.xml"/>
</beans:beans>
控制器类:
@Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
return "login";
}
@RequestMapping(value="/admin", method = RequestMethod.GET)
public String showAdmin(ModelMap model) {
return "admin";
}
@RequestMapping(value="/login", method=RequestMethod.GET)
public String enter(@ModelAttribute("employee") Employee employee, ModelMap model){
return "redirect:/admin";
}
...
用户登录和角色的表格如下:
Employee
|id|login |password |first_name|last_name|mobile_phone|role |
|1 |login1 |password1 |name1 |lname1 |phone1 |User |
|2 |login2 |password2 |name2 |lname2 |phone2 |Admin |
spring security version是4.0.3。 这是我对spring-mvc和spring-security的第一次体验,所以这个问题可能是noobie。有什么问题?
答案 0 :(得分:0)
问题是你只是重定向到/ login资源上的管理页面,如果他们没有登录就会发送给他们。由于/ admin需要一个特定的角色,它会尝试进入登录页面如果用户未登录,您已将其重定向回管理页面,从而创建循环。
您想要返回一个视图而不是重定向回管理页面。