package net.wyun.wcrs.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/resources/**", "/registration").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }
}
@@ -26,16 +26,64 @@
 urity;%0A%0A
+import javax.servlet.http.HttpServletResponse;%0A%0A
 import o
@@ -137,16 +137,16 @@
 owired;%0A
-
 import o
@@ -777,16 +777,82 @@
 Encoder;
+%0Aimport org.springframework.security.web.AuthenticationEntryPoint;
 %0A%0A@Confi
@@ -1014,16 +1014,96 @@
 Service;
+%0A    %0A    @Autowired%0A%09private AuthenticationEntryPoint authenticationEntryPoint;
 %0A%0A    @B
@@ -1689,24 +1689,109 @@
 ermitAll();%0A
+        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);%0A
     %7D%0A%0A    @
@@ -1980,15 +1980,202 @@
 der());%0A
-
     %7D%0A
+    %0A    @Bean%0A%09public AuthenticationEntryPoint unauthorizedEntryPoint() %7B%0A%09%09return (request, response, authException) -%3E response%0A%09%09%09%09.sendError(HttpServletResponse.SC_UNAUTHORIZED);%0A%09%7D%0A
 %7D

add exception handler.