【发布时间】:2016-12-17 00:34:14
【问题描述】:
我正在构建一个 Web 应用程序,它将在单个应用程序中包含一个 API 和一个管理界面。因此,我需要两种类型的身份验证,API 的基于令牌的身份验证和管理界面的基于表单的身份验证。
我几乎可以通过应用过滤器来验证 API 令牌来实现它,但是过滤器正在为每个请求执行,我只希望它在匹配“/api/**”的路径上执行。
希望从我的安全配置中可以清楚地看到我想要做什么,但遗憾的是它没有按预期工作。
所有 API 请求都将以“/api/”开头,而所有管理界面请求都将以“/admin/”开头。所以我希望对每个应用不同的安全规则。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/api/account/login").permitAll();
http.addFilterBefore(webServiceAuthenticationFilter, UsernamePasswordAuthenticationFilter.class).authorizeRequests().antMatchers("/api/**").hasAuthority("APIUSER");
http.authorizeRequests().antMatchers("/admin/**").authenticated().and()
.formLogin()
.loginPage("/admin/account/login").permitAll()
.passwordParameter("password")
.usernameParameter("username")
.failureUrl("/admin/account/login?error").permitAll()
.defaultSuccessUrl("/admin/dashboard")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/admin/account/logout"))
.logoutSuccessUrl("/admin/account/login");
http.exceptionHandling().accessDeniedPage("/admin/account/forbidden");
}
【问题讨论】:
-
我相信this 的话题可能很有趣。
标签: spring spring-security spring-boot