【问题标题】:Simple Spring Security requirement简单的 Spring Security 需求
【发布时间】:2014-08-02 09:47:36
【问题描述】:

我已经阅读了来自http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-minimal的文档

我在这里不需要角色,也不需要任何数据库。这份文件对我的要求来说太多了。我只想通过登录页面对用户进行身份验证。用户身份验证成功后,我将创建一个cookie。所有即将到来的请求都将检查它们是否具有有效的 cookie。

我的想法:会有一个过滤器检查每个即将到来的请求,如果 cookie 过期,一个入口点会将用户重定向到登录页面。

但我不知道将这些简单的要求与 Spring Security 集成......还是应该使用其他东西?

【问题讨论】:

  • 你是如何认证的?您将用户存储在哪里?您需要一些东西来存储您的用户名/密码组合。您也不需要作为选项而非要求的角色...
  • @JobSmith 你能在你的项目中使用 Spring Boot 吗?如果是这样,您所要求的可以用更少的配置来实现
  • @M. Deinum 我将使用 API 来验证凭据。
  • 然后只需提供您自己的 UserDetailsServiceAuthenticationProvider 包装此 API。
  • @M. Deinum 感谢Deinum,我很困惑。你能想出示例 xml 配置吗?再次感谢..

标签: java spring spring-security


【解决方案1】:

如果您使用的是 Java Config,那么您的 Spring Security 配置将如下所示:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private YourSpecificProvider provider; //simply wraps your API calls

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**", "/js/**", "/fonts/**", "/images/**").permitAll() //add whatever else needs to be served without authentication
                .anyRequest().authenticated();
        http
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(provider);
    }
}

通过该设置,Spring Security 将自动处理添加 cookie/检查 cookie/重定向/等所有必要步骤。 你当然必须在/login提供一个登录页面

根据您使用 Spring 的方式,您需要将 Spring Security 添加到 Servlet 过滤器链中

【讨论】:

    【解决方案2】:

    验证用户后的 Spring 安全性在 session(cookie) 上创建添加到 SecurityContextHolder 中的 Authentication 对象。

    首先您需要创建身份验证管理器

      public class RestAuthenticationProvider implements AuthenticationProvider {
    
    
       @Override
       public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        RestToken restToken = (RestToken) authentication;
    
        String key = restToken.getKey();
        String credentials = restToken.getCredentials();
    
        User user = //Here create the implementation of how to validate the user
    
    
        if(user == null){
            throw new BadCredentialsException("User does not exist");
        }
        authentication = getAuthenticatedUser(user);
        ((RestToken) authentication).setDetails(user);
        return authentication;
    }
    
    
    private Authentication getAuthenticatedUser(User user) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority(user.getRole()));
        return new RestToken(user.getId(), user.getPassword(), authorities);
    }
    
    @Override
    /*
        Determines if this class can support the token provided by the filter.
     */
    public boolean supports(Class<?> authentication) {
        return RestToken.class.equals(authentication);
    }
    

    }

    然后当您从页面收到登录信息时,调用该身份验证管理器并保存创建的身份验证对象。

                   Authentication successfulAuthentication = authenticationManager.authenticate(authentication);
                SecurityContextHolder.getContext().setAuthentication(successfulAuthentication);
    

    【讨论】:

      【解决方案3】:

      使用 Spring Security 只需最少的配置即可实现您想要的。 由于您不想将用户凭据存储在数据库中,因此可以将其以编码格式存储在配置本身中(尽管 由于安全原因,没有人应该建议您这样做 - 但这是另一个主题) 您尝试通过 cookie 执行的操作类似于 Spring 开箱即用提供的“记住我”功能。 为此,您通常需要放置一个过滤器声明/映射和一个最小配置文件和登录/注销页面,然后您就完成了。 你可以看一下简单的例子here。 如果以后您需要更多(您永远不知道 :-))您已经拥有了坚如磐石的产品。

      【讨论】:

      • 感谢 Shailendra,实际上我不会将用户名/密码存储在任何地方。一旦我知道用户名/密码匹配(可能是一个 api 调用),我就会生成一个 cookie。
      • 然后你可以很容易的将合适的 UserDetailService 插入到配置中。
      猜你喜欢
      • 2012-08-11
      • 2014-04-06
      • 2013-01-09
      • 1970-01-01
      • 2014-10-30
      • 2015-06-30
      • 1970-01-01
      • 2017-03-25
      • 2012-03-13
      相关资源
      最近更新 更多