繁体   English   中英

单用户的Spring Security

[英]Spring Security for single user

我有一个Web应用程序,其中特定的URL'/ newPost'应该只能由一个用户admin访问。 尝试导航到“ / newPost”时,应将用户重定向到登录页面,在此必须验证其身份为admin。

这一切都有效,除了用户填写登录表单时,该表单每次都会发布到“ / login”。 我现在不知道为什么它发布到'/ 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();

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

您的登录方法返回值为“ login”,这意味着它将返回至login.jsp。

用户返回“ / newPost”。

我上面的问题是一团糟。 这是我第一次尝试与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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM