package com.ryanpmartz.booktrackr.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                // allow anonymous access access to Swagger docs
                .antMatchers("/v2/api-docs", "/**/swagger-ui.html", "/webjars/**", "/swagger-resources/**").permitAll()
                // anonymous users need to be able to log in
                .antMatchers("/authenticate").permitAll()
                // all other request paths are protected
                .anyRequest().authenticated()
                .and()
                .logout()
                .permitAll();
    }

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

@@ -36,16 +36,210 @@
 onfig;%0A%0A
+import com.ryanpmartz.booktrackr.authentication.JwtAuthenticationFilter;%0Aimport com.ryanpmartz.booktrackr.authentication.JwtLoginFilter;%0Aimport com.ryanpmartz.booktrackr.authentication.JwtUtil;%0A
 import o
@@ -293,16 +293,16 @@
 owired;%0A
-
 import o
@@ -1127,16 +1127,109 @@
 Encoder;
+%0Aimport org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 %0A%0A@Confi
@@ -1440,16 +1440,138 @@
 rvice;%0A%0A
+    @Autowired%0A    private JwtAuthenticationFilter jwtAuthenticationFilter;%0A%0A    @Autowired%0A    private JwtUtil jwtUtil;%0A%0A
     @Bea
@@ -2289,16 +2289,16 @@
 ogout()%0A
-
         
@@ -2318,16 +2318,281 @@
 itAll();
+%0A%0A        http.addFilterBefore(new JwtLoginFilter(%22/authenticate%22, jwtUtil, userDetailsService, authenticationManager()), UsernamePasswordAuthenticationFilter.class);%0A        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
 %0A    %7D%0A%0A

add filters to security config