2017-12-14 15 views
2

私は私の認証実装が行われたと思っていましたが、UserDetailsオブジェクトを取得しようとすると、私はすべてユーザ名を取得しています。Spring SecurityがUserDetailsオブジェクトを返さない、ユーザ名のみ

私はoauthを使用しています。

AuthenticationManagerの設定:これが行わ付き

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 
} 

を、私は私のuserDetailsS​​erviceにデバッグすることができます。これは、罰金完了し、私のクライアントは、JWTを取り戻す

@Service 
public class UserServiceImpl implements UserService, UserDetailsService { 
@Override 
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { 
     MyUser persistedUser = userRepository.findByEmail(email); 

     if (persistedUser == null) { 
      throw new UsernameNotFoundException(String.format("The email %s doesn't exist", email)); 
     } 

     List<GrantedAuthority> authorities = new ArrayList<>(); 

     MyUser inMemoryUser = new MyUser(persistedUser.getEmail(), null, persistedUser.getEnabled(), false, 
       false, false, authorities); 

     return inMemoryUser; 
    } 
} 

。しかし、後のコントローラメソッドをデバッグするときに、次の問題が発見されました。この場合

@GetMapping 
public @ResponseBody Iterable<Curriculum> getMyCurriculums(@AuthenticationPrincipal MyUser injectedUser) { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    MyUser principle = (MyUser) auth.getPrincipal(); 
    return curriculumService.findByUser(principle); 
} 

、injectedUser =ヌル、認証はOAuth2Authenticationで、原理はStringです - ユーザ名。これはMyUserでなければなりません

+0

あなたの問題は......ですか? –

答えて

3

jwtトークンをMyUserオブジェクトにデコードするようにSpring Securityを設定する必要があります。

まず、をカプセル化するカスタムMyUserを定義します。

セキュリティ設定クラスのconfigure JWTトークン復号におけるその後
public class OAuth2AuthenticationUser extends OAuth2Authentication { 

    private MyUser myUser; 

    public OAuth2AuthenticationUser(OAuth2Request storedRequest, Authentication userAuthentication) { 
     super(storedRequest, userAuthentication); 
    } 

    public MyUser getMyUser() { 
     return myUser; 
    } 

    public void setMyUser(MyUser) { 
     this.myUser= myUser; 
    } 
} 

次のように:

@Bean 
public JwtAccessTokenConverter accessTokenConverter() { 
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 
    converter.setSigningKey("SIGNING_KEY"); 
    converter.setAccessTokenConverter(getAuthenticationAccessTokenConverter()); 
    return converter; 
} 

private DefaultAccessTokenConverter getAuthenticationAccessTokenConverter() { 
    return new DefaultAccessTokenConverter() { 
     @Override 
     public OAuth2Authentication extractAuthentication(Map<String, ?> map) { 
      OAuth2Authentication authentication = (OAuth2Authentication) super.extractAuthentication(map); 

      OAuth2AuthenticationUser authenticationUser = 
        new OAuth2AuthenticationUser(authentication.getOAuth2Request(), authentication.getUserAuthentication()); 

      MyUser myUser = new MyUser(); 

      // Example properties 
      myUser.setId(map.get("id") != null ? Long.valueOf(map.get("id").toString()) : null); 
      myUser.setUsername(map.get("user_name") != null ? map.get("user_name").toString() : null); 
      myUser.setFullName(map.get("fullName") != null ? map.get("fullName").toString() : null); 
      myUser.setCustomerId(map.get("customerId") != null ? Long.valueOf(map.get("customerId").toString()) : null); 
      myUser.setCustomerName(map.get("customerName") != null ? map.get("customerName").toString() : null); 

      // More other properties 

      authenticationUser.setMyUser(myUser); 

      return authenticationUser; 
     } 
    }; 
} 

そして、次のように、あなたは春のセキュリティコンテキストからMyUserオブジェクトへのアクセス権を持っている:

private static MyUser getMyUser() { 
    OAuth2AuthenticationUser authentication = (OAuth2AuthenticationUser) SecurityContextHolder.getContext().getAuthentication(); 
    return (authentication != null && authentication.getMyUser() != null ? authentication.getMyUser() : new MyUser()); 
} 

これをユーザーの詳細に対するデータベースアクセスが最小限に抑えられ、必要なものはすべてjwtトークンであるため、ステートレスな環境に適しています。

関連する問題