| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package cn.seecoder.fdroidrepository.config;
- import cn.seecoder.fdroidrepository.Service.ServiceImpl.UserServiceImpl;
- import cn.seecoder.fdroidrepository.security.JwtAuthenticationEntryPoint;
- import cn.seecoder.fdroidrepository.security.JwtAuthenticationTokenFilter;
- import cn.seecoder.fdroidrepository.utils.JwtUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.env.Environment;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.config.BeanIds;
- 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.builders.WebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.config.http.SessionCreationPolicy;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
- @Configuration(proxyBeanMethods = false)
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
- /**
- * The constant AUTHORIZATION_HEADER.
- */
- public static final String AUTHORIZATION_HEADER = "Authorization";
- /**
- * The constant AUTHORIZATION_TOKEN.
- */
- public static final String AUTHORIZATION_TOKEN = "access_token";
- /**
- * The constant SECURITY_IGNORE_URLS_SPILT_CHAR.
- */
- public static final String SECURITY_IGNORE_URLS_SPILT_CHAR = ",";
- /**
- * The constant TOKEN_PREFIX.
- */
- public static final String TOKEN_PREFIX = "Bearer ";
- @Autowired
- private UserServiceImpl userDetailsService;
- @Autowired
- private JwtAuthenticationEntryPoint unauthorizedHandler;
- @Autowired
- private JwtUtil tokenProvider;
- @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
- @Override
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
- }
- @Override
- public void configure(WebSecurity web) {
- String ignoreURLs = "/public/**,/open-api/**,/static/**,/css/**,/js/**,/images/**,/api/**";
- for (String ignoreURL : ignoreURLs.trim().split(SECURITY_IGNORE_URLS_SPILT_CHAR)) {
- web.ignoring().antMatchers(ignoreURL.trim());
- }
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests().anyRequest().authenticated().and()
- // custom token authorize exception handler
- .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
- // since we use jwt, session is not necessary
- .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
- // since we use jwt, csrf is not necessary
- .csrf().disable();
- http.addFilterBefore(new JwtAuthenticationTokenFilter(tokenProvider),
- UsernamePasswordAuthenticationFilter.class);
- // disable cache
- http.headers().cacheControl();
- }
- /**
- * Password encoder password encoder.
- *
- * @return the password encoder
- */
- @Bean
- public static PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- }
|