【发布时间】:2015-01-11 07:39:42
【问题描述】:
我最近创建了一个带有以下 .yo-rc.json 的 jhipster 应用程序
{
"generator-jhipster": {
"baseName": "cmpayments",
"packageName": "au.com.cmx.myapp",
"packageFolder": "au/com/cmx/myapp",
"authenticationType": "token",
"hibernateCache": "no",
"clusteredHttpSession": "no",
"websocket": "no",
"databaseType": "sql",
"devDatabaseType": "postgresql",
"prodDatabaseType": "postgresql",
"useCompass": false,
"buildTool": "maven",
"frontendBuilder": "gulp",
"javaVersion": "8"
}
}
我喜欢在 webapp 上使用基于令牌的身份验证,但我希望服务器仅使用 http 基本身份验证来公开 REST api 调用。我已经为此奋斗了一段时间,但我对 Spring 安全性完全陌生,我希望有人已经做到了这一点并且可以帮助我。
我尝试按照此处的解决方案进行操作: Basic and form based authentication with Spring security Javaconfig
我在 SecurityConfiguration.java 中使用@Order(1) 创建了第二个配置
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("api").password("pass").roles("API");
}
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/basicAuthApi/**").hasRole("API")
.and()
.httpBasic();
}
}
这行得通。如果我使用 api/pass 凭据以外的任何其他内容访问 /basicAuthApi 下的端点,我会得到 401。是的。
但是,在此之后,当我以管理员/管理员(或用户/用户)身份登录 webapp 时,我以匿名用户身份登录。如果我在 SecurityConfiguration.java 中注释掉额外的 @Configuration 并重新启动应用程序,该问题就会消失,我会以管理员(或用户)的身份正确登录。
有趣的是,我尝试将第二个@Configuration 的顺序更改为@Order(101),因为我在某个基类中的某个位置看到了@Order(100)。在这种情况下,webapp 上的管理员和用户登录可以正常工作。但是其余的 api 调用不再安全,即即使密码错误也能成功。
有谁知道我做错了什么?
谢谢 达利克
【问题讨论】:
标签: java spring-security spring-boot jhipster