WebSecurityConfigurerAdapter#configure
方法
相反,我决定将私有API中的端点与所有其他端点分开。
-
端点,需要过滤器(在我的例子中
JwtAuthenticationFilter
/api/
而且没有单独定义,因为很有可能有人忘记将它们添加到
configure
-方法
-
/原料药/
我的配置方法如下所示:
@Override
protected void configure(final HttpSecurity http) throws Exception {
//@formatter:off
http
.cors()
.and()
.csrf()
.disable()
.headers()
.frameOptions()
.disable()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js")
.permitAll()
.antMatchers("/h2-console/**").permitAll()
.antMatchers(HttpMethod.POST,
PUBLIC_API_PATH + "register",
PUBLIC_API_PATH + "login")
.permitAll()
.antMatchers(HttpMethod.GET,
PUBLIC_API_PATH + "confirm")
.permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.accessDeniedHandler(unauthorizedHandler)
.and()
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
;
//@formatter:on
JwtAuthenticationFilter
我检查请求路径是否包含私有API路径
/原料药/
. 我只应用过滤,如果它这样做。
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
if (request.getRequestURI().contains(SecurityConfiguration.PRIVATE_API_PATH)) {
// perform Jwt-authentication since request-URI suggests a call to private-API
...
}
}
filterChain.doFilter(request, response);
}
我不喜欢这个解决方案,尤其是因为我必须保持
SecurityConfiguration.PRIVATE_API_PATH
如果你有更好的建议,我很想试试。
编辑
@PostMapping(value = "${apipath}/user")
. 因此,我可以使路径毕竟是可配置的-但签入
尽管如此,我们必须保持沉默;我只是不需要使用常量,而是使用变量,其中包含来自。
application.yaml
.