Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的oauth2配置没有使用我的自定义用户服务?_Java_Spring Boot_Oauth 2.0_Spring Security Oauth2 - Fatal编程技术网

Java 为什么我的oauth2配置没有使用我的自定义用户服务?

Java 为什么我的oauth2配置没有使用我的自定义用户服务?,java,spring-boot,oauth-2.0,spring-security-oauth2,Java,Spring Boot,Oauth 2.0,Spring Security Oauth2,我正在尝试使用谷歌认证。我使用的是springboot2,所以大部分配置都是自动的。身份验证本身工作得很好,但之后我想用我自己的数据(角色、用户名等)填充主体 我已经创建了MyUserService,它扩展了DefaultOauth2UserService,并尝试按如下方式使用它: @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired MyUserS

我正在尝试使用谷歌认证。我使用的是springboot2,所以大部分配置都是自动的。身份验证本身工作得很好,但之后我想用我自己的数据(角色、用户名等)填充主体

我已经创建了MyUserService,它扩展了DefaultOauth2UserService,并尝试按如下方式使用它:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyUserService myUserService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .oauth2Login()
                .userInfoEndpoint()
                    .userService(myUserService);
    }
}
我已经和Debugger检查过了,那个应用程序从来没有实际使用loadUser方法。下面是MyUserService的实现:

@Component
public class MyUserService extends DefaultOAuth2UserService {
    @Autowired
    UserRepository userRepository;

    public MyUserService(){
        LoggerFactory.getLogger(MyUserService.class).info("initializing user service");
    }

    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        OAuth2User oAuth2User = super.loadUser(userRequest);
        Map<String, Object> attributes = oAuth2User.getAttributes();

        String emailFromGoogle = (String) attributes.get("email");
        User user = userRepository.findByEmail(emailFromGoogle);
        attributes.put("given_name", user.getFirstName());
        attributes.put("family_name", user.getLastName());

        Set<GrantedAuthority> authoritySet = new HashSet<>(oAuth2User.getAuthorities());

        return new DefaultOAuth2User(authoritySet, attributes, "sub");
    }
}
@组件
公共类MyUserService扩展了DefaultOAuth2UserService{
@自动连线
用户存储库用户存储库;
公共MyUserService(){
getLogger(MyUserService.class).info(“初始化用户服务”);
}
@凌驾
公共OAuth2User loadUser(OAuth2UserRequest userRequest)引发OAuth2AuthenticationException{
OAuth2User OAuth2User=super.loadUser(userRequest);
Map attributes=oAuth2User.getAttributes();
String emailFromGoogle=(String)attributes.get(“电子邮件”);
User=userRepository.findByEmail(emailFromGoogle);
attributes.put(“给定名称”,user.getFirstName());
attributes.put(“family_name”,user.getLastName());
Set authoritySet=newhashset(oAuth2User.getAuthories());
返回新的DefaultOAuth2User(authoritySet,attributes,“sub”);
}
}

我认为您缺少
SecurityConfig
类顶部的
@enableAuth2Client
注释


无论如何,我在这里举了一个oauth2自定义用户服务的例子,如果它有帮助的话,实际上解决方案只是为google身份验证添加另一个属性:

spring.security.oauth2.client.registration.google.scope=profile email

不确定默认作用域是什么,以及为什么服务的入口取决于作用域,但没有这一行代码,我的定制服务就永远不会收到代码。

非常感谢!虽然这不是解决方案,但你的项目确实帮助我解决了问题。我将用单独的答案来回答,这样很容易发现是否有人会遇到同样的问题。答案是,默认情况下,如果在配置Google OAuth2客户端时没有提供任何作用域,Spring Boot将使用CommonOAuth2Provider类中提供的默认值。默认范围是:openid、profile、email。通过包含openid范围,Spring Boot将使用OAuth2UserService接口的OidcUserService实现,而不是默认的OAuth2UserService。所以你应该扩展客户服务。并对其进行配置