【发布时间】:2016-08-14 00:24:02
【问题描述】:
我有一个 Web 应用程序,其中一个特定的 url '/newPost' 应该只能由一个用户访问,即管理员。当试图导航到“/newPost”时,用户应该被重定向到一个登录页面,他必须在该页面验证他作为管理员的身份。
这一切都有效,除了用户填写登录表单时,表单每次都会发布到“/登录”。我目前不知道为什么它发布到“/ login”而不是我使用百里香叶重定向到的路径:th:action="@{/newPost}"
TLDR;提交登录表单后,我不断被重定向到 /login。我正在使用 Spring boot、Spring security 和 thymeleaf。
控制器:
/*Keeps getting called*/
@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(Model model)
{
model.addAttribute("lc", new LoginCredentials());
System.out.println("Login controller");
return "login";
}
/*RequestMapping I want to be called*/
@RequestMapping(value="/newPost", method = RequestMethod.GET)
public String isLoggedIn(@ModelAttribute LoginCredentials lc, Model model)
{
if(lc.isAdmin())
{
System.out.println("lc is admin");
model.addAttribute("bp",new BlogPost());
return "newPost";
} else
{
System.out.println("lc is not admin");
return "login";
}
}
登录表格:
<form class="form-signin" th:action="@{/newPost}" th:object="${lc}" method = "post">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Username</label>
<input type="text" id="username" class="form-control" th:field="*{inputUsername}" placeholder="Username" required="required" autofocus="autofocus" />
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="password" th:field="*{inputPsswd}" class="form-control" placeholder="Password" required ="required" />
<button class="btn btn-lg btn-primary btn-block" type="submit" style="background-color:#F6358A;">Sign in</button>
</form>
安全配置:
httpSecurity
.authorizeRequests()
.antMatchers("/","/videos","/BlogPost","/index","/aboutUs").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
【问题讨论】:
-
您的表单是 post 方法,它具有指向 newPost 的操作 url。您可以添加有关该方法的更多详细信息吗?另外,你发布的表单是登录表单,那么你在哪里配置 Spring Security 来处理登录信息呢?
-
因为您没有使用 Spring Security 但正在解决它...
-
@M.Deinum 感谢您的评论。它促使我阅读更多 Spring Security 文档,学习它,然后真正解决问题。
标签: java spring spring-security spring-boot thymeleaf