2016-04-21 87 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