【发布时间】:2018-06-27 14:18:12
【问题描述】:
我的 spring-boot 项目基于 REST 和 JWT 身份验证。现在我想禁用特定路径的 JWT 身份验证,而不是通过简单的用户名和密码进行身份验证。可以实现吗?
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint())
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/login/**").permitAll()
.antMatchers("/register/**").permitAll()
.antMatchers("/api/jwt/**").authenticated()
.anyRequest().authenticated().and()
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class).csrf().disable();
}
我想通过用户名和密码添加身份验证,例如添加到“/api/data/**”。
编辑:
我的第一个配置是@Order(1)
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("abc").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/data/**").hasRole("ADMIN")
.and()
.httpBasic()
.and()
.csrf().disable();
}
它可以工作,但是当执行对 api/data/** 具有正确凭据的请求时,jwtAuthenticationFilter() 也会触发。
【问题讨论】:
标签: spring authentication spring-security