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

import com.rizki.mufrizal.belajar.oauth2.service.UserDetailLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity(debug = true)
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailLoginService userDetailLoginService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .inMemoryAuthentication()
                .and()
                .userDetailsService(userDetailLoginService)
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/api/SaveUser").permitAll()
                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .and()
                .logout()
                .logoutUrl("/j_spring_security_logout");
    }

}

@@ -52,16 +52,76 @@
 urity;%0A%0A
+import com.rizki.mufrizal.belajar.oauth2.filter.CORSFilter;%0A
 import c
@@ -1163,16 +1163,96 @@
 Encoder;
+%0Aimport org.springframework.security.web.access.channel.ChannelProcessingFilter;
 %0A%0A@Confi
@@ -1270,24 +1270,27 @@
 nableWeb
+Mvc
 Security
 (debug =
@@ -1285,22 +1285,8 @@
 rity
-(debug = true)
 %0A@En
@@ -1288,35 +1288,32 @@
 y%0A@EnableWeb
-Mvc
 Security
 %0A@EnableGlob
@@ -1292,32 +1292,46 @@
 nableWebSecurity
+(debug = true)
 %0A@EnableGlobalMe
@@ -1359,16 +1359,39 @@
 nabled =
+ true, prePostEnabled =
  true)%0Ap
@@ -2303,32 +2303,36 @@
                 
+    
 .antMatchers(%22/l
@@ -2336,16 +2336,33 @@
 %22/login%22
+, %22/api/SaveUser%22
 ).permit
@@ -2359,32 +2359,36 @@
 r%22).permitAll()%0A
+    
                 
@@ -2406,37 +2406,38 @@
 s(%22/api/
-Save
 User%22).
-permitAll(
+hasRole(%22ADMIN%22
 )%0A      
@@ -2469,16 +2469,20 @@
         
+    
 .formLog
@@ -2482,24 +2482,32 @@
 formLogin()%0A
+        
             
@@ -2543,24 +2543,32 @@
             
+        
 .loginProces
@@ -2603,16 +2603,76 @@
 check%22)%0A
+                        .failureUrl(%22/login?error%22)%0A        
         
@@ -2703,21 +2703,23 @@
 er(%22
-j_username%22)%0A
+email%22)%0A       
     
@@ -2722,32 +2722,33 @@
                 
+ 
 .passwordParamet
@@ -2751,18 +2751,16 @@
 ameter(%22
-j_
 password
@@ -2777,32 +2777,36 @@
          .and()%0A
+    
                 
@@ -2827,24 +2827,32 @@
             
+        
 .logoutUrl(%22
@@ -2878,16 +2878,349 @@
 logout%22)
+%0A                        .logoutSuccessUrl(%22/login?logout%22)%0A                .and()%0A                    .csrf()%0A                .and()%0A                    .exceptionHandling()%0A                        .accessDeniedPage(%22/403%22)%0A                .and()%0A                    .addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class)
 ;%0A    %7D%0A

Update Spring Security Config