rest - Spring Security 为两个不同的 url 配置两个不同的身份验证
问题描述
我正在开发一个带有 spring 安全性的 web 应用程序,它将被 web 用户和 android 用户使用。目前我已经配置WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/","/about","/contact","/v1/**").permitAll()
.antMatchers("/admin/otp").hasRole("PRE_AUTH_USER")
.antMatchers("/admin/**").hasAnyRole("SuperAdmin","BackOffice")
.antMatchers("/admin/dashboard/**").hasAnyRole("BankAdmin","CallAdmin")
//.antMatchers("/api/**").hasRole("FieldPerson")
.anyRequest().authenticated();
http.formLogin()
.loginPage("/admin/login")
.permitAll()
.defaultSuccessUrl("/admin/otp", true)
.and()
.logout()
.logoutSuccessUrl("/admin/login")
.permitAll();
http.exceptionHandling().accessDeniedPage("/error/403");
}
@Bean
public DaoAuthenticationProvider authProvider(){
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(new BCryptPasswordEncoder(12));
return authProvider;
}
@Autowired
public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService).and().authenticationProvider(authProvider());
}
}
这很完美,但我想在没有任何 otp 验证屏幕的情况下使用相同的配置进行 rest api 身份验证(基本上它是一个带有 GOOGLE Authenticator 的网络用户的 2FA)。所以我想为android用户配置数据库身份验证。我尝试了这个解决方案,但我因user is not anonymous
. 基本上我只想在用户点击它时配置/api/**
它应该被授权。
解决方案
推荐阅读
- php - 棘轮套筒 ws 到 wss
- flyte - 您可以为 staging 和 prod 的工作流程制定单独的时间表吗?
- node.js - MongoDB“无法读取 null 的属性‘构造函数’”
- java - Spring Boot 应用程序错误 Jslt/jsp 标签给出异常
- javascript - 大家好, 如何将 Javascript 数组转换为对象
- kubernetes-helm - Helm - 无法从私有 Github 存储库安装依赖项
- javascript - 为什么这个反转条件动画不起作用?
- spring - 请求范围 bean 异常和 @ControllerAdvice
- javascript - 过滤后如何保留 HTML 表格的第一行?
- html - 如何从浏览器复制标签元素