java - 403 访问被拒绝 Spring Security 每次
问题描述
对于每个具有模式 /admin 的请求,我都会收到 403 我只需要将 /admin 限制为管理员角色。
失败的方法:
- 尝试在控制器上使用
@PreAuthorize(hasRole('ADMIN'))
和@PreAuthorize(hasRole('ROLE_ADMIN'))
但没有运气。 - 尝试
@PreAuthorize
从控制器中删除并使用 hasRole 在下面的类中添加模式但没有运气
下面是类扩展WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
AuthenticationEntryPoint authenticationEntryPoint;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers(HttpMethod.GET,"/admin/**").hasAnyRole("ADMIN","ADMIN_TENANT")
.anyRequest().authenticated()
.and()
.logout()
.permitAll()
.and()
.csrf()
.disable();
httpSecurity.
addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
httpSecurity.
headers().cacheControl().disable();
}
已经尝试过类似问题中提到的解决方案,但没有运气。所以请不要将其标记为重复。
解决方案
我类似的 Git 项目:这里
// Config to check if already active
http.authorizeRequests()
.and()
.rememberMe()
.tokenRepository(new JdbcTokenRepositoryImpl().setDataSource(//datasource_name) )
.tokenValiditySeconds(//TimeInterval in sec);
}
推荐阅读
- python-3.x - 如何获取包含 api 密钥的 YAML 文件以在 django 中工作?
- python - Xcode 更新破坏了 gcloud 和 gsutil
- javascript - 黄貂鱼 - 不改变 URL 的交通脚本
- rust - 如何访问 web_sys 中的导航器或其他基于 Web 的资源?
- flutter - 将pageview放在listview中
- oracle - 我的存储过程的错误代码 ORA-00933
- scikit-learn - 在 SVM 上运行 sklearn.metrics.plot_roc_curve 时变化的参数是什么?
- javascript - 创建空构造函数和在类中初始化数组时出现问题
- firebase - 从所有 firebase 文档中获取 lat 和 long
- sql - 更新时间序列数据中的大量行