|
|
|
@ -1,113 +1,47 @@
|
|
|
|
|
package com.shxy.xymanager_service.service.security;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.shxy.xymanager_common.security.componet.CustomAuthExceptionEntryPoint;
|
|
|
|
|
|
|
|
|
|
import com.shxy.xymanager_service.impl.CustomUserDetailsServiceImpl;
|
|
|
|
|
import com.shxy.xymanager_service.impl.LogoutSuccessHandlerImpl;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
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.configuration.EnableWebSecurity;
|
|
|
|
|
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;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableWebSecurity// 这个注解必须加,开启Security
|
|
|
|
|
@EnableGlobalMethodSecurity(prePostEnabled = true)//保证post之前的注解可以使用
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private CustomAuthExceptionEntryPoint customAuthExceptionEntryPoint;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private CustomUserDetailsServiceImpl customUserDetailsServiceImpl;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
|
|
|
|
/**
|
|
|
|
|
* 退出处理类
|
|
|
|
|
*/
|
|
|
|
|
@Autowired
|
|
|
|
|
private LogoutSuccessHandlerImpl logoutSuccessHandler;
|
|
|
|
|
public class CustomSecurityConfig implements WebMvcConfigurer {
|
|
|
|
|
|
|
|
|
|
private final JwtInterceptor jwtInterceptor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//先来这里认证一下
|
|
|
|
|
@Autowired
|
|
|
|
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
|
|
log.info("213123123");
|
|
|
|
|
auth.userDetailsService(customUserDetailsServiceImpl).passwordEncoder(passwordEncoderBean());
|
|
|
|
|
public CustomSecurityConfig(JwtInterceptor jwtInterceptor) {
|
|
|
|
|
this.jwtInterceptor = jwtInterceptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//拦截在这配
|
|
|
|
|
@Override
|
|
|
|
|
protected void configure(HttpSecurity httpSecurity) throws Exception {
|
|
|
|
|
log.info("123566");
|
|
|
|
|
httpSecurity
|
|
|
|
|
// 认证失败处理类
|
|
|
|
|
.exceptionHandling().authenticationEntryPoint(customAuthExceptionEntryPoint).and()
|
|
|
|
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
|
|
|
|
|
// 过滤请求
|
|
|
|
|
.authorizeRequests()
|
|
|
|
|
// 对于登录login 验证码captchaImage 允许匿名访问
|
|
|
|
|
.antMatchers("/login", "/captchaImage").anonymous()
|
|
|
|
|
.antMatchers("*/api/addUser").anonymous()
|
|
|
|
|
.antMatchers("**/addUser").anonymous()
|
|
|
|
|
.antMatchers("/addUser").anonymous()
|
|
|
|
|
.antMatchers(
|
|
|
|
|
HttpMethod.GET,
|
|
|
|
|
"/*.html",
|
|
|
|
|
"/**/*.html",
|
|
|
|
|
"/**/*.css",
|
|
|
|
|
"/**/*.js"
|
|
|
|
|
).permitAll()
|
|
|
|
|
.antMatchers("/profile/**").anonymous()
|
|
|
|
|
.antMatchers("/common/download**").anonymous()
|
|
|
|
|
.antMatchers("/swagger-ui.html").anonymous()
|
|
|
|
|
.antMatchers("/swagger-resources/**").anonymous()
|
|
|
|
|
.antMatchers("/webjars/**").anonymous()
|
|
|
|
|
.antMatchers("/*/api-docs").anonymous()
|
|
|
|
|
.antMatchers("/druid/**").anonymous()
|
|
|
|
|
.antMatchers("/test/**").anonymous()
|
|
|
|
|
.antMatchers( "/v3/**").anonymous()
|
|
|
|
|
.antMatchers( "/error").anonymous()
|
|
|
|
|
// .antMatchers("/**").anonymous()
|
|
|
|
|
.antMatchers("/api/**").anonymous()
|
|
|
|
|
// .antMatchers("/busiMainDeviceController/**").anonymous()
|
|
|
|
|
.antMatchers("/sysLoginRemoteController/**").anonymous()
|
|
|
|
|
// .antMatchers("/remote/**").anonymous()
|
|
|
|
|
.antMatchers("/userRemote/**").anonymous()
|
|
|
|
|
// 除上面外的所有请求全部需要鉴权认证
|
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
|
.and()
|
|
|
|
|
.headers().frameOptions().disable();
|
|
|
|
|
|
|
|
|
|
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
|
|
|
|
// 添加JWT filter
|
|
|
|
|
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public PasswordEncoder passwordEncoderBean() {
|
|
|
|
|
return new BCryptPasswordEncoder();
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
registry.addInterceptor(this.jwtInterceptor).addPathPatterns("/getXcxMainInfo").addPathPatterns("/authorizeXcxLogin")
|
|
|
|
|
.addPathPatterns("/bindXcxDevice")
|
|
|
|
|
.excludePathPatterns("/login")
|
|
|
|
|
.excludePathPatterns("/api/addUser")
|
|
|
|
|
.excludePathPatterns("/swagger-ui.html")
|
|
|
|
|
.excludePathPatterns("/swagger-resources/**")
|
|
|
|
|
.excludePathPatterns("/*/api-docs")
|
|
|
|
|
.excludePathPatterns("/error")
|
|
|
|
|
.excludePathPatterns("/test/**")
|
|
|
|
|
.excludePathPatterns("/loginXcxuser")
|
|
|
|
|
.excludePathPatterns("/loginXcxuser")
|
|
|
|
|
.excludePathPatterns("/addUser");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@Override
|
|
|
|
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
|
|
|
return super.authenticationManagerBean();
|
|
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
|
registry.addMapping("/**")
|
|
|
|
|
.allowedOrigins("*")
|
|
|
|
|
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
|
|
|
|
|
.maxAge(3600)
|
|
|
|
|
.allowCredentials(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|