public void configure(HttpSecurity http) throws Exception { http. // 关闭csrf csrf().disable() // 跨域处理过滤器 .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class) .addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class) // 授权异常 .exceptionHandling() // 防止iframe 造成跨域 .and() .headers() .frameOptions() .disable() // 不创建会话 .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() // swagger 文档 .antMatchers("/swagger-ui.html").permitAll() .antMatchers("/swagger-resources/**").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/*/api-docs").permitAll() .antMatchers("/doc.html").permitAll() // 指定接口不进行验证 .antMatchers("/login").permitAll() .antMatchers("/websocket").permitAll() .antMatchers("/token/refresh").permitAll() .antMatchers("/config/listSystemPage").permitAll() .antMatchers("/img/**").permitAll() // 所有请求都需要认证 .anyRequest().authenticated(); }
时间: 2023-06-18 12:04:10 浏览: 84
这是一个使用Spring Security进行Web应用程序安全控制的Java代码片段,配置了HttpSecurity对象,包括关闭csrf、添加跨域处理过滤器和授权异常处理等。其中,使用了antMatchers()方法指定特定的URL模式,例如"/login"、"/websocket"等,这些URL不需要进行验证,而其他所有请求都需要进行认证。同时,还允许访问Swagger文档和静态资源。最后,使用了sessionCreationPolicy()方法指定不创建会话,使用了authenticated()方法指定所有请求都需要认证。
相关问题
@Slf4j @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //配置认证管理器 @Bean//添加此注解是为了能够在Controller中自动装配 @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } //配置密码的加密方式 @Bean public PasswordEncoder passwordEncoder(){ // 下面代码是获取了一个无加密的实例 return NoOpPasswordEncoder.getInstance(); } @Override protected void configure(HttpSecurity http) throws Exception { // super.configure(http); //配置自己的登陆页面, 当判断没有登录的时候 自动跳转到自己的登陆页面 http.formLogin().loginPage("/login.html"); //配置白名单(无需登录也能访问) String[] urls = {"/reg.html","/login.html","/reg","login"}; http.authorizeRequests() .mvcMatchers(urls)//匹配某些路径 .permitAll()//直接放行(不需要登录可以访问) .anyRequest()//其他请求 .authenticated();//需要登陆认证才能访问 // 关闭默认开启的跨域攻击防御 http.csrf().disable(); } }
这段代码是一个Spring Security的配置类,用于配置认证和授权规则。下面是对代码的一些解释:
1. `@Slf4j`:这是一个Lombok注解,用于自动生成日志对象。
2. `@Configuration`:这是一个Spring注解,表示这是一个配置类。
3. `SecurityConfig`:这个类继承了`WebSecurityConfigurerAdapter`类,用于自定义Spring Security的配置。
4. `authenticationManager()`方法:这个方法被`@Bean`注解标记,表示将返回的`AuthenticationManager`对象注册为一个Bean,以供其他组件自动装配使用。
5. `passwordEncoder()`方法:这个方法也被`@Bean`注解标记,表示将返回的`PasswordEncoder`对象注册为一个Bean,用于配置密码的加密方式。在这里使用的是`NoOpPasswordEncoder.getInstance()`,即不对密码进行加密处理。
6. `configure(HttpSecurity http)`方法:这个方法用于配置Spring Security的认证和授权规则。在这里,配置了自定义的登录页面、白名单和关闭跨域攻击防御。
需要注意的是,这段代码中的密码加密方式使用了一个不安全的实例`NoOpPasswordEncoder.getInstance()`,仅用于演示目的。在实际项目中,应该使用安全的密码加密算法来保护用户密码。
springboot security跨域怎么处理 例如前端使用端口为8080 请求后端使用的端口8088
在Spring Security中,可以通过配置CorsFilter来处理跨域请求。具体做法如下:
1. 在Spring Boot项目中添加依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2. 创建CorsFilter配置类
```java
@Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
};
}
}
```
3. 配置Spring Security
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/api/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
```
以上代码中,我们使用了`WebMvcConfigurerAdapter`来配置CorsFilter,允许所有来源的请求,并指定允许的HTTP方法、请求头和Credentials。在Spring Security中,我们使用`http.cors()`来启用CorsFilter,同时禁用了CSRF防护。
需要注意的是,由于跨域请求中会先发送OPTIONS请求,因此我们需要在Spring Security中允许OPTIONS请求。上述示例中,我们使用`antMatchers(HttpMethod.OPTIONS).permitAll()`来允许所有的OPTIONS请求。