How to get all information of authenticated user in Resource server oauth2












0















I have an monolithic application that is configured by spring security. There is User user = (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() code to get authenticated user and this code is worked correctly.

After migration to oauth2 with the following configuration:



@EnableResourceServer
@EnableWebSecurity
@Configuration
public class ResourcesServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.GET, "/api/**").access("#oauth2.hasScope('read')");
}

@Primary
@Bean
public RemoteTokenServices tokenService() {
RemoteTokenServices tokenService = new RemoteTokenServices();
tokenService.setCheckTokenEndpointUrl("http://localhost:8081/auth/account/getDetailUser");
tokenService.setClientId("web");
tokenService.setClientSecret("secret");
return tokenService;
}


And the its application.yml is:



spring:
datasource:
url: jdbc:oracle:thin:@192.168.192.129:1521:hamed
hikari:
connection-test-query: SELECT 1 FROM DUAL
minimum-idle: 1
maximum-pool-size: 5
driver-class-name: oracle.jdbc.OracleDriver
username: test
password: test
initialization-mode: always
jpa:
hibernate:
ddl-auto: none
database-platform: org.hibernate.dialect.Oracle12cDialect
logging:
level:
org.springframework.security: DEBUG

server:
port: 8083
context-path: /micro1
security:
basic:
enabled: false
oauth2:
client:
clientId: web
clientSecret: secret
accessTokenUri: http://localhost:8081/auth/oauth/token
userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
resource:
userInfoUri: http://localhost:8081/auth/account/getDetailUser


The (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() does not work in Resource server. Another hand how to get from authorization server the authority and other information of autheticated user in Resource Server.










share|improve this question



























    0















    I have an monolithic application that is configured by spring security. There is User user = (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() code to get authenticated user and this code is worked correctly.

    After migration to oauth2 with the following configuration:



    @EnableResourceServer
    @EnableWebSecurity
    @Configuration
    public class ResourcesServerConfiguration extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .antMatchers(HttpMethod.GET, "/api/**").access("#oauth2.hasScope('read')");
    }

    @Primary
    @Bean
    public RemoteTokenServices tokenService() {
    RemoteTokenServices tokenService = new RemoteTokenServices();
    tokenService.setCheckTokenEndpointUrl("http://localhost:8081/auth/account/getDetailUser");
    tokenService.setClientId("web");
    tokenService.setClientSecret("secret");
    return tokenService;
    }


    And the its application.yml is:



    spring:
    datasource:
    url: jdbc:oracle:thin:@192.168.192.129:1521:hamed
    hikari:
    connection-test-query: SELECT 1 FROM DUAL
    minimum-idle: 1
    maximum-pool-size: 5
    driver-class-name: oracle.jdbc.OracleDriver
    username: test
    password: test
    initialization-mode: always
    jpa:
    hibernate:
    ddl-auto: none
    database-platform: org.hibernate.dialect.Oracle12cDialect
    logging:
    level:
    org.springframework.security: DEBUG

    server:
    port: 8083
    context-path: /micro1
    security:
    basic:
    enabled: false
    oauth2:
    client:
    clientId: web
    clientSecret: secret
    accessTokenUri: http://localhost:8081/auth/oauth/token
    userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
    resource:
    userInfoUri: http://localhost:8081/auth/account/getDetailUser


    The (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() does not work in Resource server. Another hand how to get from authorization server the authority and other information of autheticated user in Resource Server.










    share|improve this question

























      0












      0








      0








      I have an monolithic application that is configured by spring security. There is User user = (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() code to get authenticated user and this code is worked correctly.

      After migration to oauth2 with the following configuration:



      @EnableResourceServer
      @EnableWebSecurity
      @Configuration
      public class ResourcesServerConfiguration extends ResourceServerConfigurerAdapter {
      @Override
      public void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests()
      .antMatchers(HttpMethod.GET, "/api/**").access("#oauth2.hasScope('read')");
      }

      @Primary
      @Bean
      public RemoteTokenServices tokenService() {
      RemoteTokenServices tokenService = new RemoteTokenServices();
      tokenService.setCheckTokenEndpointUrl("http://localhost:8081/auth/account/getDetailUser");
      tokenService.setClientId("web");
      tokenService.setClientSecret("secret");
      return tokenService;
      }


      And the its application.yml is:



      spring:
      datasource:
      url: jdbc:oracle:thin:@192.168.192.129:1521:hamed
      hikari:
      connection-test-query: SELECT 1 FROM DUAL
      minimum-idle: 1
      maximum-pool-size: 5
      driver-class-name: oracle.jdbc.OracleDriver
      username: test
      password: test
      initialization-mode: always
      jpa:
      hibernate:
      ddl-auto: none
      database-platform: org.hibernate.dialect.Oracle12cDialect
      logging:
      level:
      org.springframework.security: DEBUG

      server:
      port: 8083
      context-path: /micro1
      security:
      basic:
      enabled: false
      oauth2:
      client:
      clientId: web
      clientSecret: secret
      accessTokenUri: http://localhost:8081/auth/oauth/token
      userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
      resource:
      userInfoUri: http://localhost:8081/auth/account/getDetailUser


      The (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() does not work in Resource server. Another hand how to get from authorization server the authority and other information of autheticated user in Resource Server.










      share|improve this question














      I have an monolithic application that is configured by spring security. There is User user = (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() code to get authenticated user and this code is worked correctly.

      After migration to oauth2 with the following configuration:



      @EnableResourceServer
      @EnableWebSecurity
      @Configuration
      public class ResourcesServerConfiguration extends ResourceServerConfigurerAdapter {
      @Override
      public void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests()
      .antMatchers(HttpMethod.GET, "/api/**").access("#oauth2.hasScope('read')");
      }

      @Primary
      @Bean
      public RemoteTokenServices tokenService() {
      RemoteTokenServices tokenService = new RemoteTokenServices();
      tokenService.setCheckTokenEndpointUrl("http://localhost:8081/auth/account/getDetailUser");
      tokenService.setClientId("web");
      tokenService.setClientSecret("secret");
      return tokenService;
      }


      And the its application.yml is:



      spring:
      datasource:
      url: jdbc:oracle:thin:@192.168.192.129:1521:hamed
      hikari:
      connection-test-query: SELECT 1 FROM DUAL
      minimum-idle: 1
      maximum-pool-size: 5
      driver-class-name: oracle.jdbc.OracleDriver
      username: test
      password: test
      initialization-mode: always
      jpa:
      hibernate:
      ddl-auto: none
      database-platform: org.hibernate.dialect.Oracle12cDialect
      logging:
      level:
      org.springframework.security: DEBUG

      server:
      port: 8083
      context-path: /micro1
      security:
      basic:
      enabled: false
      oauth2:
      client:
      clientId: web
      clientSecret: secret
      accessTokenUri: http://localhost:8081/auth/oauth/token
      userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
      resource:
      userInfoUri: http://localhost:8081/auth/account/getDetailUser


      The (User)SecuritycontextHolder.getContext().getAuthentication().getPrincipal() does not work in Resource server. Another hand how to get from authorization server the authority and other information of autheticated user in Resource Server.







      spring-boot oauth-2.0 authorization spring-security-oauth2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 15:08









      reza ramezani matinreza ramezani matin

      4041622




      4041622
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277782%2fhow-to-get-all-information-of-authenticated-user-in-resource-server-oauth2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277782%2fhow-to-get-all-information-of-authenticated-user-in-resource-server-oauth2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre