【问题标题】:Spring Security for single user单用户的 Spring Security
【发布时间】: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


【解决方案1】:

您的登录页面 jsp 名称是什么?那是“login.jsp”吗?

你的登录方法返回的是“login”,也就是说它会返回到login.jsp。

用户返回“/newPost”。

【讨论】:

  • 我没有用jsps,我用的是thymeleaf。
【解决方案2】:

我上面的问题是一团糟。这是我第一次尝试使用 Java Spring 并且我的问题显示。我希望这个解释对未来的用户有所帮助。

首先:

该操作不应与 /login 不同。我本质上导致了登录的无限循环,因为我通过提交登录表单将用户发送到 /newPost,但是在他们提供正确的凭据之前,他们无法访问 /newPost。 Spring 尽职尽责地将用户重定向到 /login 以提供正确的凭据,重复该过程。

这个:

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();
  }
}

第三:

由于我更改了 Springs 全局配置,我不应该将对象传递给 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>

【讨论】:

    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 2021-10-27
    • 2014-10-27
    • 2016-05-03
    • 2014-08-12
    • 2016-06-26
    • 1970-01-01
    • 2012-02-28
    相关资源
    最近更新 更多