Spring Security 怎么实现普通用户和后台管理员认证分离?

2016-11-30 15:44:08 +08:00
 wwha
我想用 Spring Security 实现普通用户使用一个登录入口和一个 AuthenticationProvider ,后台管理员使用另一个一个登录入口和另一个 AuthenticationProvider 。但是 Spring 官网上说,所有的 AuthenticationProvider 都是由 ProviderManager 管理的,有多个 provider ,他们将会被按顺序进行尝试(内部使用了一个 List),每个 provider 都可以尝试进行认证,或者简单的通过返回 null 来跳过认证。直到认证通过为止。我想问能不能一个 HttpSecurity 对应一个 AuthenticationProvider ,这样就不用做多余尝试了。

现在我在一个类中定义了两个 HttpSecurity,请问有什么解决方法吗?:

@Configuration
public static class UserLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/")
...
}

//管理员登录配置
@Configuration
@Order(1)
public static class AdminWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
...

@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/admin/**")
...
}
}
2096 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/324361

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX