| | |
| | | package com.ruoyi.framework.config; |
| | | |
| | | import com.ruoyi.framework.config.properties.SecurityProperties; |
| | | import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; |
| | | import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; |
| | | import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; |
| | | import de.codecentric.boot.admin.server.config.AdminServerProperties; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.http.HttpMethod; |
| | |
| | | |
| | | /** |
| | | * spring security配置 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) |
| | | public class SecurityConfig extends WebSecurityConfigurerAdapter |
| | | { |
| | | public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| | | /** |
| | | * 自定义用户认证逻辑 |
| | | */ |
| | | @Autowired |
| | | private UserDetailsService userDetailsService; |
| | | |
| | | |
| | | /** |
| | | * 认证失败处理类 |
| | | */ |
| | |
| | | private CorsFilter corsFilter; |
| | | |
| | | @Autowired |
| | | private AdminServerProperties adminServerProperties; |
| | | |
| | | private SecurityProperties securityProperties; |
| | | |
| | | /** |
| | | * 解决 无法直接注入 AuthenticationManager |
| | | * |
| | |
| | | */ |
| | | @Bean |
| | | @Override |
| | | public AuthenticationManager authenticationManagerBean() throws Exception |
| | | { |
| | | public AuthenticationManager authenticationManagerBean() throws Exception { |
| | | return super.authenticationManagerBean(); |
| | | } |
| | | |
| | |
| | | * authenticated | 用户登录后可访问 |
| | | */ |
| | | @Override |
| | | protected void configure(HttpSecurity httpSecurity) throws Exception |
| | | { |
| | | protected void configure(HttpSecurity httpSecurity) throws Exception { |
| | | httpSecurity |
| | | // CSRF禁用,因为不使用session |
| | | .csrf().disable() |
| | | // 认证失败处理类 |
| | | .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() |
| | | // 基于token,所以不需要session |
| | | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() |
| | | // 过滤请求 |
| | | .authorizeRequests() |
| | | // 对于登录login 验证码captchaImage 允许匿名访问 |
| | | .antMatchers("/login", "/captchaImage").anonymous() |
| | | .antMatchers( |
| | | HttpMethod.GET, |
| | | "/*.html", |
| | | "/**/*.html", |
| | | "/**/*.css", |
| | | "/**/*.js" |
| | | ).permitAll() |
| | | .antMatchers("/profile/**").anonymous() |
| | | .antMatchers("/common/download**").anonymous() |
| | | .antMatchers("/common/download/resource**").anonymous() |
| | | .antMatchers("/swagger-ui.html").anonymous() |
| | | .antMatchers("/swagger-resources/**").anonymous() |
| | | .antMatchers("/webjars/**").anonymous() |
| | | .antMatchers("/*/api-docs").anonymous() |
| | | .antMatchers("/druid/**").anonymous() |
| | | // Spring Boot Admin Server 的安全配置 |
| | | .antMatchers(adminServerProperties.getContextPath()).anonymous() |
| | | .antMatchers(adminServerProperties.getContextPath() + "/**").anonymous() |
| | | // Spring Boot Actuator 的安全配置 |
| | | .antMatchers("/actuator").anonymous() |
| | | .antMatchers("/actuator/**").anonymous() |
| | | // 除上面外的所有请求全部需要鉴权认证 |
| | | .anyRequest().authenticated() |
| | | .and() |
| | | .headers().frameOptions().disable(); |
| | | httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); |
| | | // CSRF禁用,因为不使用session |
| | | .csrf().disable() |
| | | // 认证失败处理类 |
| | | .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() |
| | | // 基于token,所以不需要session |
| | | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() |
| | | // 过滤请求 |
| | | .authorizeRequests() |
| | | .antMatchers( |
| | | HttpMethod.GET, |
| | | "/", |
| | | "/*.html", |
| | | "/**/*.html", |
| | | "/**/*.css", |
| | | "/**/*.js" |
| | | ).permitAll() |
| | | .antMatchers(securityProperties.getAnonymous()).anonymous() |
| | | .antMatchers(securityProperties.getPermitAll()).permitAll() |
| | | // 除上面外的所有请求全部需要鉴权认证 |
| | | .anyRequest().authenticated() |
| | | .and() |
| | | .headers().frameOptions().disable(); |
| | | httpSecurity.logout().logoutUrl(securityProperties.getLogoutUrl()).logoutSuccessHandler(logoutSuccessHandler); |
| | | // 添加JWT filter |
| | | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| | | // 添加CORS filter |
| | |
| | | httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 强散列哈希加密实现 |
| | | */ |
| | | @Bean |
| | | public BCryptPasswordEncoder bCryptPasswordEncoder() |
| | | { |
| | | public BCryptPasswordEncoder bCryptPasswordEncoder() { |
| | | return new BCryptPasswordEncoder(); |
| | | } |
| | | |
| | |
| | | * 身份认证接口 |
| | | */ |
| | | @Override |
| | | protected void configure(AuthenticationManagerBuilder auth) throws Exception |
| | | { |
| | | protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| | | auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); |
| | | } |
| | | } |