我有一個web應用程序,其中一個特定的url'/ newPost'應該只能被一個用戶admin訪問。當試圖導航到'/ newPost'時,應該將用戶重定向到登錄頁面,在那裏他必須以管理員身份驗證他的身份。
這一切都有效,除了當用戶填寫登錄表單時,表單會每次發送到「/ login」。我目前不知道它爲什麼發佈到'/ login'而不是我重定向到使用百里香葉子的路徑:th:action="@{/newPost}"
Spring Security for single user
TLDR;提交登錄表單後,我不斷被重定向到/登錄。我正在使用春季靴子,春季安全和百里香。
控制器:
/*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文檔,學習它,然後真正解決問題。 – Turtle