Java Spring安全性:仅当尚未通过表单登录进行身份验证时,才对REST api进行身份验证
全部, 我有一个带有MVC和REST端点的spring boot应用程序。web应用程序的用户首先使用下面的基于表单的身份验证配置登录。还有一些暴露的REST端点将由外部应用程序调用——这些端点使用下面的JWT第二配置进行身份验证 一旦用户登录,我想调用REST端点/api/**而不通过JWT进行身份验证,因为用户已经通过基于表单的auth登录。因此,我基本上只想在用户未经身份验证的情况下使用JWT身份验证SecurityContext没有身份验证。我不知道如何才能做到这一点 非常感谢您的帮助 谢谢Java Spring安全性:仅当尚未通过表单登录进行身份验证时,才对REST api进行身份验证,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,全部, 我有一个带有MVC和REST端点的spring boot应用程序。web应用程序的用户首先使用下面的基于表单的身份验证配置登录。还有一些暴露的REST端点将由外部应用程序调用——这些端点使用下面的JWT第二配置进行身份验证 一旦用户登录,我想调用REST端点/api/**而不通过JWT进行身份验证,因为用户已经通过基于表单的auth登录。因此,我基本上只想在用户未经身份验证的情况下使用JWT身份验证SecurityContext没有身份验证。我不知道如何才能做到这一点 非常感谢您的帮助
@Configuration
@Order(1)
public static class UserRole1ConfigAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatcher(new NegatedRequestMatcher(new AntPathRequestMatcher("/api/**")))
.authorizeRequests()
.antMatchers("/userrole1/**")
.hasRole(Role.USERROLE1.name())
.and()
.formLogin()
.loginPage("/userrole1/login")
.permitAll()
.loginProcessingUrl("/userrole1/login")
.defaultSuccessUrl("/userrole1/dashboard")
.and()
.logout()
.logoutUrl("/userrole1/logout")
.logoutSuccessUrl("/userrole1/login")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.permitAll();
}
}
你已经有这个了
.antMatchers/api/寄存器
permitAll先生
这将允许未经验证的请求。
您还需要什么呢?/api/register只是一个URL,所有URL都应该允许。你到底还想找什么如果使用基于表单的身份验证对用户进行身份验证,我希望在不再次通过JWT进行身份验证的情况下调用/api/userrole1/**端点。REST是无状态的,因此不能使用表单登录。表单登录使用HTTP会话。
@Configuration
@Order(2)
public static class RestConfigAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/register")
.permitAll()
.antMatchers("/api/userrole1/**")
.hasRole(Role.USERROLE1.name())
.antMatchers("/api/userrole2/**")
.hasRole(Role.USERROLE2.name())
.addFilter(new JwtAuthenticationFilter(authenticationManager(), jwtUtil, jwtConfig))
.addFilter(new JwtAuthorizationFilter(jwtUtil, jwtConfig, userDetailsService, authenticationManager()))
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf()
.disable();
}
}