Spring Security Authentication Provider

Spring Security Authentication Provider 介绍

本文介绍spring security如何相比简单的UserDetailService上实现灵活的认证。

Authentication Provider

spring security提供了多种执行认证方式,但都遵循一个简单的约定——AuthenticationProvider处理认证请求,然后返回授信后的完整认证对象。

标准的、最常用的实现是DaoAuthenticationProvider——从只读user Dao中返回 user details。user details仅通过username发挥整个用户实体,大多数场景这完全可以满足。

很多特定场景仍然需要访问完整的认证请求去执行认证过程——举例,当对一些外部第三方认证服务,认证请求需要用户名和密码,两者都是必须的。

针对这些情况,我们需要自定义Authentication Provider。

@Component
public class CustomAuthenticationProvider
  implements AuthenticationProvider {
 
    @Override
    public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException {
  
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
         
        if (shouldAuthenticateAgainstThirdPartySystem()) {
  
            // use the credentials
            // and authenticate against the third-party system
            return new UsernamePasswordAuthenticationToken(
              name, password, new ArrayList<>());
        } else {
            return null;
        }
    }
 
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(
          UsernamePasswordAuthenticationToken.class);
    }
}

注意,上面代码中返回的认证对象中授权权限是空,因为实际中应该有特定应用给提供。

注册Authentication Provider

现在Authentication Provider已经定义好了,我们需要在Security Configuration中进行注册,可以使用xml和javaConfig方式,两者意义都一样,下面给出javaConfig方式代码:

@Configuration
@EnableWebSecurity
@ComponentScan("org.baeldung.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
    @Autowired
    private CustomAuthenticationProvider authProvider;
 
    @Override
    protected void configure(
      AuthenticationManagerBuilder auth) throws Exception {
  
        auth.authenticationProvider(authProvider);
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated()
            .and()
            .httpBasic();
    }
}

测试认证

对客户端来说,服务器端是否为自定义Authentication Provider都一样,我们使用简单的curl命令行工具(windows用户可以通过git bash)发送认证请求:

curl --header "Accept:application/json" -i --user user1:user1Pass 
    http://localhost:8080/spring-security-custom/api/foo/1

上面示例中我们使用http基本认证保护rest api。因此从服务器端返回期望的200。

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B8F0EFA81B78DE968088EBB9AFD85A60; Path=/spring-security-custom/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 02 Jun 2013 17:50:40 GMT

总结

本文讨论了如何在spring Security中自定义Authentication Provider。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值