2017-11-19 4 views
-1
@RestController 
public class AuthenticationController { 

    @RequestMapping("/") 
    protected Principal login(Principal user) { 
     ObjectMapper mapper = new ObjectMapper(); 

      System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal()); 
      System.out.println(SecurityContextHolder.getContext().getAuthentication().getDetails()); 
      System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal()); 
      System.out.println("testing testing xyz"); 
     return user; 
    } 
} 

これは私のコードです。私はユーザーの詳細を得るために可能な限り最大限の方法で試してきました。実際に私はユーザーの電子メールが欲しいですが、 "user"(プリンシパルオブジェクト)を返すときに、スクリーンにjsonを与えています。 、...それを通過し、私は任意のものが間違って作られたなら、私が知っている...と私の範囲はOpenIDので下さい春の起動時にプリンシパルオブジェクトからユーザーの詳細を取得できませんOAuth 2

を追加しました春のセキュリティ設定。これで私を助けて、電子メール、

package com.ggktech; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Configurable; 
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; 
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.oauth2.client.OAuth2ClientContext; 
import org.springframework.security.oauth2.client.OAuth2RestTemplate; 
import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter; 
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; 
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; 
import org.springframework.security.web.csrf.CookieCsrfTokenRepository; 

/** 
* Modifying or overriding the default spring boot security. 
*/ 
@Configurable 
@EnableWebSecurity 
public class OAuthSecurityConfig extends WebSecurityConfigurerAdapter { 

    private OAuth2ClientContext oauth2ClientContext; 
    private AuthorizationCodeResourceDetails authorizationCodeResourceDetails; 
    private ResourceServerProperties resourceServerProperties; 

    @Autowired 
    public void setOauth2ClientContext(OAuth2ClientContext oauth2ClientContext) { 
     this.oauth2ClientContext = oauth2ClientContext; 
    } 

    @Autowired 
    public void setAuthorizationCodeResourceDetails(AuthorizationCodeResourceDetails authorizationCodeResourceDetails) { 
     this.authorizationCodeResourceDetails = authorizationCodeResourceDetails; 
    } 

    @Autowired 
    public void setResourceServerProperties(ResourceServerProperties resourceServerProperties) { 
     this.resourceServerProperties = resourceServerProperties; 
    } 

    /* This method is for overriding the default AuthenticationManagerBuilder. 
    We can specify how the user details are kept in the application. It may 
    be in a database, LDAP or in memory.*/ 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     super.configure(auth); 
    } 

    /* This method is for overriding some configuration of the WebSecurity 
    If you want to ignore some request or request patterns then you can 
    specify that inside this method.*/ 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     super.configure(web); 
    } 

    /*This method is used for override HttpSecurity of the web Application. 
    We can specify our authorization criteria inside this method.*/ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http 
       // Starts authorizing configurations. 
       .authorizeRequests() 
       // Ignore the "/" and "/index.html" 
       .antMatchers("/", "/**.html", "/**.js").permitAll() 
       // Authenticate all remaining URLs. 
       .anyRequest().fullyAuthenticated() 
       .and() 
       // Setting the logout URL "/logout" - default logout URL. 
       .logout() 
       // After successful logout the application will redirect to "/" path. 
       .logoutSuccessUrl("/") 
       .permitAll() 
       .and() 
       // Setting the filter for the URL "/google/login". 
       .addFilterAt(filter(), BasicAuthenticationFilter.class) 
       .csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); 
    } 

    /*This method for creating filter for OAuth authentication.*/ 
    private OAuth2ClientAuthenticationProcessingFilter filter() { 
     //Creating the filter for "/google/login" url 
     OAuth2ClientAuthenticationProcessingFilter oAuth2Filter = new OAuth2ClientAuthenticationProcessingFilter(
       "/login"); 

     //Creating the rest template for getting connected with OAuth service. 
     //The configuration parameters will inject while creating the bean. 
     OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(authorizationCodeResourceDetails, 
       oauth2ClientContext); 
     oAuth2Filter.setRestTemplate(oAuth2RestTemplate); 

     // Setting the token service. It will help for getting the token and 
     // user details from the OAuth Service. 
     oAuth2Filter.setTokenServices(new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), 
       resourceServerProperties.getClientId())); 

     return oAuth2Filter; 
    } 
} 
+0

のために有用であろうあなたの構成についての適切な詳細を提供してください。 oAuthで春のセキュリティ設定を表示 – Hiren

+0

完了、一度それを行って – Dev

答えて

-1

をプロファイリングしてくださいメソッドはRESTエンドポイントです。つまり、この関数に渡されるパラメータはシリアル化されたデータです。デシリアライズして必要なデータを取得する必要があります。このファンクションのパラメータはPricipleタイプではありません。送信した場所から、おそらくbyte[]に送信する必要があります。次に、byte[]String(JSON)に変換する必要があります。次にJacksonライブラリを使用してuserを入力する必要があります。その後、ユーザーの電子メールを取得することができます。

@RequestMapping("/") 
protected Principal login(byte[] data) { 
    String inputJSONString = new String(data); 
    ObjectMapper mapper = new ObjectMapper(); 
    Principle user = objectMapper.readValue(inputJSONString, Principle.class); 
    //Now you have a setted user object and you can get user's mail from a method like getMail() 
    user.getMail(); 

    System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal()); 
    System.out.println(SecurityContextHolder.getContext().getAuthentication().getDetails()); 
    System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal(
    System.out.println("testing testing xyz"); 
    return user; 
} 
+0

こんにちは、お返事ありがとうございます。コンパイルエラーが発生しています。 "user" - > Principalは戻り値の型で、与えられた解では 'User user'となります。 – Dev

+0

私はPrincipleクラスをあなたのユーザタイプと見なします。解決策を修正しました。もう一度やり直せますか? –

+0

あなたは理解できません...プリンシパルは春のセキュリティクラスで、問題は春のセキュリティのテキストから基本的な詳細を得ています – Hiren

0

問題は、あなたがこの@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { super.configure(auth); }

認証マネージャ行っているあなたのコードであなたのAuthenticationManagerを設定していないです:完全装備を返し、渡された認証対象を認証する

試みを認証オブジェクト(許可された権限を含む)が成功した場合。

単純なメモリ認証マネージャでは、このようなことができます。

@Autowired 
public void configure(AuthenticationManagerBuilder auth) 
     throws Exception { 
    auth.inMemoryAuthentication().withUser("user").password("password") 
      .roles("USER").and().withUser("hiren").password("hiren") 
      .roles("ADMIN"); 
} 

この後、Principalオブジェクトは、ユーザーの認証に成功した後に取得できます。また、このような独自の認証プロバイダを構成することができ :

@Override 
protected void configure(
    AuthenticationManagerBuilder auth) throws Exception { 

    auth.authenticationProvider(customeAuthenticationProvider); 
} 

thisリンクは、認証プロバイダの設定

関連する問題