88a3a3beef4d6b7878b3fcddd559190685a33e4a.svn-base 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package org.jeecg.monitor.config;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  7. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  8. /**
  9. * @author scott
  10. */
  11. @Configuration
  12. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  13. private final String adminContextPath;
  14. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  15. this.adminContextPath = adminServerProperties.getContextPath();
  16. }
  17. @Override
  18. protected void configure(HttpSecurity http) throws Exception {
  19. // 登录成功处理类
  20. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  21. successHandler.setTargetUrlParameter("redirectTo");
  22. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  23. http.authorizeRequests()
  24. //静态文件允许访问
  25. .antMatchers(adminContextPath + "/assets/**").permitAll()
  26. //登录页面允许访问
  27. .antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
  28. //其他所有请求需要登录
  29. .anyRequest().authenticated()
  30. .and()
  31. //登录页面配置,用于替换security默认页面
  32. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  33. //登出页面配置,用于替换security默认页面
  34. .logout().logoutUrl(adminContextPath + "/logout").and()
  35. .httpBasic().and()
  36. .csrf()
  37. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  38. .ignoringAntMatchers(
  39. "/instances",
  40. "/actuator/**"
  41. );
  42. }
  43. }