2016-04-21 82 views
0

我有一個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(); 
+0

您的表單是post方法,它具有對newPost的操作url。你可以添加更多有關該方法的細節?另外,您發佈的表單是登錄表單,因此您在哪裏配置Spring Security來處理登錄信息? –

+1

因爲你沒有使用Spring Security,但正在解決它... –

+0

@ M.Deinum感謝您的評論。它促使我閱讀更多Spring Security文檔,學習它,然後真正解決問題。 – Turtle

回答

0

我上面的問題很混亂。這是我第一次嘗試使用Java Spring和我的問題展示。我希望這個解釋有助於未來的用戶。

首先:

動作不應該從/登錄不同。我基本上造成了無限循環的登錄,因爲我通過提交登錄表單將用戶發送到/ newPost,但他們在提供正確憑證之前無法訪問/ newPost。春天忠實地將用戶重定向到/登錄,以提供正確的憑據,重複此過程。

此:

th:action="@{/newPost}" 

應該是:

th:action="@{/login}" 

與相應RequestMapping像這樣:

@RequestMapping(value="/login", method=RequestMethod.POST) 
public String loginPost(Model model) 
{ 
//do foo 
} 

其次:

我試圖爲Spring Security做這件事。

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"; 
} 

因爲我只需要爲單個用戶的安全,我應該在我的安全配置配置的AuthenticationManagerBuilder對象,像這樣:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) 
{ 
    try 
    { 
    auth 
     .inMemoryAuthentication() 
      .withUser("admin") 
      .password("password") 
      .roles("ADMIN"); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 

三:

因爲我改變了泉全局配置,我不應該把一個對象傳遞給login.html。新表單應使用如下輸入字段:

<form class="form-signin" th:action="@{/login}" method = "post"> 
    <h2 class="form-signin-heading">Please sign in</h2> 
    <label for="inputEmail" class="sr-only">Username</label> 
    <input type="text" id="username" name="username" class="form-control" placeholder="Username" required="required" autofocus="autofocus" /> 
    <label for="inputPassword" class="sr-only">Password</label> 
    <input type="password" id="password" name="password" 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> 
0

什麼是你的登錄頁面的jsp的名字嗎?那是「login.jsp」嗎?

您的登錄方法返回是「登錄」,這意味着它將返回到login.jsp。

用戶改爲「/ newPost」。

+0

我不使用jsps,我正在使用百里香。 – Turtle