【发布时间】:2016-05-04 13:16:15
【问题描述】:
这是我第一次尝试 Spring Security,所以我的问题的最终解决方案可能很简单。所以... 我有一个索引页面,它只有登录表单:
<form name="loginForm" action="login.html" method="post">
<table>
<tr>
<td><@spring.message "form.email" /></td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td><@spring.message "form.password" /></td>
<td><input type="password" name="password" /></td
</tr>
</table>
<input type="submit" value="${signIn}" />
</form>
<form name="" action="createAccount.html">
<input type="submit" value="${register}" />
</form>
当我发出 POST 请求时,它由控制器处理。在我的控制器中,我从数据库中检索 UserAccount 数据并将其传递到另一个名为“account.html”的页面。
这个控制器方法贴在下面:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String logUserAccount(@RequestParam(value = "email", required = false) String email,
@RequestParam(value = "password", required = false) String password, RedirectAttributes redirect) {
try {
UserAccount userAccount = userAccountService.signIn(email, password);
redirect.addFlashAttribute("userAccount", userAccount);
return "redirect:account.html";
} catch (InvalidCreditnailsException e) {
return RedirectController.REDIRECT_TO_INDEX_VIEW;
}
}
和 Next 控制器方法,将用户帐户数据建模并渲染 account.html 页面:
@RequestMapping(value = "/account", method = RequestMethod.GET)
public String accountWindow(@ModelAttribute("userAccount") UserAccount userAccount, Model model){
model.addAttribute("userAccount", userAccount);
return "account";
}
现在,我想保护 account.html 页面,防止非授权用户直接访问 /account.html 页面。但是我的 Spring Security 配置不正确。它看起来像这样:
<security:http>
<security:intercept-url pattern="/account**" access="ROLE_USER" />
<security:form-login
login-page="/index.html"
login-processing-url="/login.html"
default-target-url="/account.html"
username-parameter="email"
password-parameter="password"
/>
<security:logout />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="test@gmail.com" password="qwerty" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
它实际上是做什么的?当我尝试直接访问 /account.html 时,它会将我重定向到我有登录表单的 index.html。没关系。但是当我登录 Spring Security 时直接将我重定向到 /account.html 页面,而不是向我的登录控制器发送 /login.html 请求以检索用户数据。
如何设置> 有什么想法吗?也许我的方法不正确?我只希望所有客人都可以使用索引和注册页面。页面的其余部分仅适用于登录用户。
感谢您的帮助。
【问题讨论】:
标签: spring spring-mvc spring-security