package com.rizki.mufrizal.belajar.oauth2.configSecurity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

@Configuration
public class Oauth2Config {

    private static final String RESOURCE_ID = "BelajarOauth2";

    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

        @Autowired
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
            authorizationServerEndpointsConfigurer
                    .tokenStore(new InMemoryTokenStore())
                    .authenticationManager(authenticationManager);
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception {
            authorizationServerSecurityConfigurer.checkTokenAccess("hasRole('CLIENT')");
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
            clientDetailsServiceConfigurer
                    .inMemory()
                    .withClient("clientauthcode")
                    .secret("123456")
                    .authorizedGrantTypes("authorization_code", "refresh_token")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .and()
                    .withClient("clientcred")
                    .secret("123456")
                    .authorizedGrantTypes("client_credentials")
                    .scopes("trust")
                    .resourceIds(RESOURCE_ID)
                    .and()
                    .withClient("clientapp")
                    .secret("123456")
                    .authorizedGrantTypes("password")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .and()
                    .withClient("angularClient")
                    .secret("angularPassword")
                    .authorizedGrantTypes("implicit")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .authorities("CLIENT")
                    .redirectUris("http://localhost:20000/implicit-client/")
                    .accessTokenValiditySeconds(60 * 60 * 24) // token berlaku seharian, besok harus login ulang
                    .autoApprove(true);
        }

    }

}

@@ -209,40 +209,161 @@
 ork.
-context.annotation.Configuration
+beans.factory.annotation.Value;%0Aimport org.springframework.context.annotation.Configuration;%0Aimport org.springframework.context.annotation.PropertySource
 ;%0Aim
@@ -1095,16 +1095,66 @@
 uration%0A
+@PropertySource(%22classpath:urlClient.properties%22)%0A
 public c
@@ -1238,16 +1238,91 @@
 uth2%22;%0A%0A
+    @Value(%22$%7BangularProject.url%7D%22)%0A    private static String angularUrl;%0A%0A
     @Con
@@ -2153,16 +2153,103 @@
 NT')%22);%0A
+            authorizationServerSecurityConfigurer.checkTokenAccess(%22hasRole('USER')%22);%0A
         
@@ -2468,32 +2468,36 @@
                 
+    
 .withClient(%22cli
@@ -2522,32 +2522,40 @@
                 
+        
 .secret(%22123456%22
@@ -2568,32 +2568,40 @@
                 
+   
+     
 .authorizedGrant
@@ -2657,32 +2657,40 @@
                 
+        
 .scopes(%22read%22, 
@@ -2710,32 +2710,40 @@
                 
+ 
+       
 .resourceIds(RES
@@ -2791,32 +2791,36 @@
                 
+    
 .withClient(%22cli
@@ -2841,32 +2841,40 @@
                 
+    
+    
 .secret(%22123456%22
@@ -2867,32 +2867,40 @@
 ecret(%22123456%22)%0A
+        
                 
@@ -2959,32 +2959,40 @@
                 
+        
 .scopes(%22trust%22)
@@ -3004,255 +3004,8 @@
     
-            .resourceIds(RESOURCE_ID)%0A                    .and()%0A                    .withClient(%22clientapp%22)%0A                    .secret(%22123456%22)%0A                    .authorizedGrantTypes(%22password%22)%0A                    .scopes(%22read%22, %22write%22)%0A
     
@@ -3085,32 +3085,36 @@
                 
+ 
+   
 .withClient(%22ang
@@ -3138,32 +3138,40 @@
                 
+        
 .secret(%22angular
@@ -3193,32 +3193,40 @@
                 
+   
+     
 .authorizedGrant
@@ -3255,32 +3255,40 @@
                 
+        
 .scopes(%22read%22, 
@@ -3308,32 +3308,40 @@
                 
+ 
+       
 .resourceIds(RES
@@ -3362,32 +3362,40 @@
                 
+       
+ 
 .authorities(%22CL
@@ -3399,18 +3399,32 @@
 %22CLIENT%22
-)%0A
+, %22USER%22)%0A      
         
@@ -3435,16 +3435,18 @@
         
+  
 .redirec
@@ -3455,51 +3455,28 @@
 ris(
-%22http://localhost:20000/implicit-client/%22)%0A
+angularUrl)%0A        
     
@@ -3539,57 +3539,29 @@
 ) //
- token berlaku seharian, besok harus login ulang%0A
+untuk sehari%0A        
     

Update Oauth2 Config