如何使用spring boot security实现身份验证?

时间:2014-05-18 12:29:38

标签: spring-boot

我正在使用春季靴子。我想发布用户名和密码params登录,如果登录成功,则返回一个令牌。之后,我将使用令牌来判断登录状态。这是我的安全配置代码。但我不知道在哪里写登录验证逻辑代码。

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {



     @Override
     protected void configure(HttpSecurity http) throws Exception {
         http.authorizeRequests()
                .anyRequest()
                .fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/user/unlogin")
                .permitAll(); 
     }

     @Override
     public void configure(WebSecurity web) throws Exception {
         web.ignoring().antMatchers("/user/login")
                .antMatchers("/user/logout")
                .antMatchers("/user/register"); 
     }
}

==========================

谢谢你!

1 个答案:

答案 0 :(得分:3)

使用Spring总是有不止一种方法可以做。 Spring Boot有一条快乐的道路(可能),你似乎已经开始了。但请注意,如果您希望Boot提供某些默认行为,请不要使用@EnableWebSecurity(如用户指南中所述)。 web-secure sample有一个您可以遵循的示例。

如果您使用formLogin(),则默认登录网址为/login(不是/user/login),因此您应该能够将用户名和密码发布到该端点进行身份验证。不要使用/loginweb.ignoring()添加到不安全的路径,否则Spring Security将永远不会处理它。当您发布到/login时,您会收到JSESSIONID Cookie。这是您的身份验证令牌,当会话在服务器中到期时它会过期(默认为30分钟,但可以轻松配置)。将其包含在将来对安全资源的请求中 - 一些HTTP客户端甚至会为您执行此操作(如浏览器一样)。