java - 在spring boot中需要认证才能获取公共资源
问题描述
我需要经过身份验证才能获取公共资源。
这种结构对我有用,即使我经过身份验证,我也只能获得我的公共资源。当我在通过身份验证后尝试例如发布请求时,我得到 403 禁止。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/private/**").authenticated()
.antMatchers("/private-scoped").hasAuthority("read:posts");
}
使用此配置,我需要经过身份验证才能访问这些资源中的任何一个,甚至是公共资源。当我通过身份验证时,一切正常。
@Override
protected void configure(HttpSecurity http) throws Exception {
JwtWebSecurityConfigurer
.forRS256(audience, issuer)
.configure(http)
.cors()
.and()
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/private/**").authenticated()
.antMatchers("/private-scoped").hasAuthority("read:posts");
}
有没有可能将这两种配置合二为一的方法?
我从我的前端发送 jwt 令牌。
这是我的配置文件:
import com.auth0.spring.security.api.JwtWebSecurityConfigurer;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
/**
*
* @author Kamil
*/
@Configuration
@EnableWebSecurity
public class AuthConfig extends WebSecurityConfigurerAdapter {
@Value(value = "${auth0.apiAudience}")
private String audience;
@Value(value = "${auth0.issuer}")
private String issuer;
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.applyPermitDefaultValues();
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedOrigins(Arrays.asList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
JwtWebSecurityConfigurer
.forRS256(audience, issuer)
.configure(http)
.cors()
.and()
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/private/**").authenticated()
.antMatchers("/private-scoped").hasAuthority("read:posts");
}
}
解决方案
试试这个方法:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/private-scoped").hasAuthority("read:posts")
.anyRequest().authenticated()
.and()
.cors();
}
推荐阅读
- java - 制作一个将在Android中使用的Java库,一个类实现Parcelable接口
- flutter - 有没有更好的方法来不断重建一个小部件?
- javascript - 为什么“this”不等于调用函数的对象?
- c++ - 使用多个sampler2D一次使用一个发送多个纹理?
- html - CSS 弹性盒 | 修复 Flex 列高度之间的空间
- file - 无法在 codeigniter 中显示文件验证错误消息
- python - 从 axios 传输到 django 的文件大小发生变化
- java - 仅显示数组中的唯一项
- bash - join 命令将匹配字段作为新行插入,如何为每个匹配生成单行?
- bash - 在 Bash 中,当它已经被重定向时如何打印到标准输出?