SecurityConfiguration.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.common.workflow.config;
  2. import com.common.workflow.security.*;
  3. import com.common.workflow.security.jwt.*;
  4. import org.springframework.beans.factory.BeanInitializationException;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Import;
  8. import org.springframework.http.HttpMethod;
  9. import org.springframework.security.authentication.AuthenticationManager;
  10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  11. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  12. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  13. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  14. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  15. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  16. import org.springframework.security.config.http.SessionCreationPolicy;
  17. import org.springframework.security.core.userdetails.UserDetailsService;
  18. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  19. import org.springframework.security.crypto.password.PasswordEncoder;
  20. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  21. import org.springframework.web.filter.CorsFilter;
  22. import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport;
  23. import javax.annotation.PostConstruct;
  24. @Configuration
  25. @EnableWebSecurity
  26. @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
  27. @Import(SecurityProblemSupport.class)
  28. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  29. private final AuthenticationManagerBuilder authenticationManagerBuilder;
  30. private final UserDetailsService userDetailsService;
  31. private final TokenProvider tokenProvider;
  32. private final CorsFilter corsFilter;
  33. private final SecurityProblemSupport problemSupport;
  34. public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService, TokenProvider tokenProvider, CorsFilter corsFilter, SecurityProblemSupport problemSupport) {
  35. this.authenticationManagerBuilder = authenticationManagerBuilder;
  36. this.userDetailsService = userDetailsService;
  37. this.tokenProvider = tokenProvider;
  38. this.corsFilter = corsFilter;
  39. this.problemSupport = problemSupport;
  40. }
  41. @PostConstruct
  42. public void init() {
  43. try {
  44. authenticationManagerBuilder
  45. .userDetailsService(userDetailsService)
  46. .passwordEncoder(passwordEncoder());
  47. } catch (Exception e) {
  48. throw new BeanInitializationException("Security configuration failed", e);
  49. }
  50. }
  51. @Override
  52. @Bean
  53. public AuthenticationManager authenticationManagerBean() throws Exception {
  54. return super.authenticationManagerBean();
  55. }
  56. @Bean
  57. public PasswordEncoder passwordEncoder() {
  58. return new BCryptPasswordEncoder();
  59. }
  60. @Override
  61. public void configure(WebSecurity web) throws Exception {
  62. web.ignoring()
  63. .antMatchers(HttpMethod.OPTIONS, "/**")
  64. .antMatchers("/swagger-ui/index.html")
  65. .antMatchers("/test/**");
  66. }
  67. @Override
  68. public void configure(HttpSecurity http) throws Exception {
  69. http
  70. .csrf()
  71. .disable()
  72. .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
  73. .exceptionHandling()
  74. .authenticationEntryPoint(problemSupport)
  75. .accessDeniedHandler(problemSupport)
  76. .and()
  77. .headers()
  78. .frameOptions()
  79. .disable()
  80. .and()
  81. .sessionManagement()
  82. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  83. .and()
  84. .authorizeRequests()
  85. .antMatchers("/api/acti/**").permitAll()
  86. .antMatchers("/api/apose/**").permitAll()
  87. .antMatchers("/api/word/**").permitAll()
  88. .antMatchers("/api/register").permitAll()
  89. .antMatchers("/api/activate").permitAll()
  90. .antMatchers("/api/authenticate").permitAll()
  91. .antMatchers("/api/account/reset-password/init").permitAll()
  92. .antMatchers("/api/account/reset-password/finish").permitAll()
  93. .antMatchers("/api/**").authenticated()
  94. .antMatchers("/management/health").permitAll()
  95. .antMatchers("/management/info").permitAll()
  96. .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
  97. .and()
  98. .apply(securityConfigurerAdapter());
  99. }
  100. private JWTConfigurer securityConfigurerAdapter() {
  101. return new JWTConfigurer(tokenProvider);
  102. }
  103. }