首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户成功登录GitHub后,如何获取用户详细信息?我想将一些字段保存到数据库中

用户成功登录GitHub后,如何获取用户详细信息?我想将一些字段保存到数据库中
EN

Stack Overflow用户
提问于 2019-08-10 01:47:57
回答 1查看 509关注 0票数 1

当用户使用Github登录时,我试图从用户信息中获取一些字段。我想要获取的信息是'login','bio','url‘。如何将特定字段保存到DB?

当我使用Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); authentication.getPrincipal()时,我得到了所有用户的信息,而不知道如何提取特定的字段。这是我得到的回复:

代码语言:javascript
复制
    Name: [18630847], Granted Authorities: [ROLE_USER], User Attributes: 
    [login=Blahblah, id=123456789, node_id=4tfg4fg43g, 
    avatar_url=https://avatars1.githubusercontent.com/u/12345678?v=4, 
    gravatar_id=, url=https://api.github.com/users/Blahblah, 
    html_url=https://github.com/blahblah, 
    followers_url=https://api.github.com/users/blahblah/followers, ...... 
    private_repos=10000}]

我也尝试将此转换为Map,但没有实现。

代码语言:javascript
复制
# Databases
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=ruslan

# GitHub
spring.security.oauth2.client.registration.github.client-id=${github-id}
spring.security.oauth2.client.registration.github.client-secret=${github-secret}
代码语言:javascript
复制
    @GetMapping("/loginSuccess")
    public String getLoginInfo(Model model) {
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        model.addAttribute("userd", authentication.getPrincipal().toString());
        return "client";
    }
代码语言:javascript
复制
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                .oauth2Login()
                .defaultSuccessUrl("/loginSuccess")
                .and()
                .logout().logoutSuccessUrl("/index")//permitall
                .and()
                    .csrf().disable()
                    .headers().frameOptions().disable();

    }
代码语言:javascript
复制
public interface UserRepository extends JpaRepository<User, Long> {

//    User findByUsername(String username);

}
代码语言:javascript
复制
@Setter
@Getter
@Entity
public class User {

    @Id
    @GeneratedValue
    private long id;

    private String login;

//    private String password;

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    private List<Role> roles;

    public User(String login, String password) {
        this.login = login;
    }

我想保存用户的特定字段,如'login','name','bio‘到我的数据库中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-10 22:21:34

主体对象应为OAuth2AuthenticationToken的实例

代码语言:javascript
复制
@GetMapping("/loginSuccess")
public String getLoginInfo(Model model) {
    Authentication authentication = SecurityContextHolder.getContext()
            .getAuthentication();

    //Add this:
    Object principal = authentication.getPrincipal();
    if(principal instanceof OAuth2AuthenticationToken){
      OAuth2AuthenticationToken oAuth2AuthenticationToken = (OAuth2AuthenticationToken)principal;
     //By default its DefaultOAuth2User.
     OAuth2User oAuth2User = oAuth2AuthenticationToken.getPrincipal();
    }

     Map<String,Object> attributes =  oAuth2User.getAttributes();

    //now you can retrieve all attribute values you are interested from attributes map and store or return to view...


   // your logic here



    return "client";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57434960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档