Created
January 14, 2019 09:36
-
-
Save mohashari/ffe7e216db0eee82724f3e3dcc3f48ff to your computer and use it in GitHub Desktop.
WebSecurityConfig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.monggopesen.mainservice.config; | |
| 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.web.builders.HttpSecurity; | |
| import org.springframework.security.config.annotation.web.builders.WebSecurity; | |
| 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 javax.servlet.http.HttpServletResponse; | |
| @EnableWebSecurity | |
| @Configuration | |
| public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
| @Autowired | |
| private UserDetailsService userDetailsService; | |
| @Override | |
| public void configure(WebSecurity web) throws Exception { | |
| } | |
| @Bean | |
| @Override | |
| public AuthenticationManager authenticationManagerBean() throws Exception { | |
| return super.authenticationManagerBean(); | |
| } | |
| @Override | |
| protected void configure(HttpSecurity http) throws Exception { | |
| http.csrf().disable() | |
| .exceptionHandling() | |
| .authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> | |
| httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED)) | |
| .and().authorizeRequests() | |
| .antMatchers("/**").permitAll() | |
| .and().httpBasic(); | |
| } | |
| @Bean | |
| BCryptPasswordEncoder bCryptPasswordEncoder() { | |
| return new BCryptPasswordEncoder(); | |
| } | |
| @Override | |
| protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
| auth.userDetailsService(userDetailsService) | |
| .passwordEncoder(bCryptPasswordEncoder()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment