【问题标题】:Spring boot Basic Authentication and OAuth2 in same project?Spring boot Basic Authentication和OAuth2在同一个项目中?
【发布时间】:2018-10-08 06:08:04
【问题描述】:

是否可以在我的休息应用程序中为某些端点使用 OAuth2,并为其他一些端点使用基本身份验证。 它应该在 spring security 版本 2.0.1.RELEASE 上工作。我希望有人可以进一步帮助我。

【问题讨论】:

    标签: java maven spring-boot intellij-idea spring-security


    【解决方案1】:

    是的,可以同时使用基本身份验证和 OAuth2 身份验证,但我怀疑您是否能够轻松设置它,因为 HttpSecurity 的 authenticated() 方法不允许您选择您的身份验证中的哪一个方法(oauth2Login/formLogin)将起作用。

    但是,有一种方法可以轻松绕过:

    您可以添加自定义权限,当用户使用基本身份验证连接时,我们称之为 ROLE_BASICAUTH,当用户使用 OAuth2 连接时,我们称之为 ROLE_OAUTH2。这样,您可以使用

    .antMatchers("/endpoint-that-requires-basic-auth").hasRole("BASICAUTH")
    .antMatchers("/endpoint-that-requires-oauth2").hasRole("OAUTH2")
        .anyRequest().authenticated()
    

    当他们到达您需要基本身份验证(而不是 OAuth2)的端点时,您检查他们当前的权限,如果不是 BASICAUTH,那么您将显示他们的会话无效,您会显示一个登录表单没有 OAuth2 em>(强制他们使用基本身份验证)。

    这样做的缺点是您需要同时实现自定义 UserDetailsService 和自定义 OAuth2UserService...

    但这实际上并不难:

    @Service
    public class UserService extends DefaultOAuth2UserService implements UserDetailsService {
    
        // ...
    
        @Override
        public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
            OAuth2User user = super.loadUser(oAuth2UserRequest);
    
            Map<String, Object> attributes = user.getAttributes();
            Set<GrantedAuthority> authoritySet = new HashSet<>(user.getAuthorities());
            String userNameAttributeName = oAuth2UserRequest.getClientRegistration().getProviderDetails()
                    .getUserInfoEndpoint().getUserNameAttributeName();
    
            authoritySet.add(new SimpleGrantedAuthority("ROLE_OAUTH2"));
    
            return new DefaultOAuth2User(authoritySet, attributes, userNameAttributeName);
        }
    
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UserDetails user = getUserFromDatabase(username); // you'll need to provide that method (where are the username/password stored?)
            if (user == null) { // UserDetailsService doesn't allow loadUserByUsername to return null, so throw exception
                throw new UsernameNotFoundException("Couldn't find user with username '"+username+"'");
            }
            // add ROLE_BASICAUTH (you might need a custom UserDetails implementation here, because by defaut, UserDetails.getAuthorities() is immutable (I think, I might be a liar)
            return user;
        }
    
    }
    

    请注意,这是一个粗略的实现,因此您还必须自己完成一些工作。

    您还可以使用我创建的这个存储库 https://github.com/TwinProduction/spring-security-oauth2-client-example/tree/master/custom-userservice-sample 作为自定义 OAuth2UserService 的指南

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2023-04-06
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2016-10-22
      • 2022-01-25
      • 2020-08-23
      • 2017-02-02
      相关资源
      最近更新 更多