java – spring-boot在单个Web应用程序路径上设置基本身份验证?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – spring-boot在单个Web应用程序路径上设置基本身份验证?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3886字,纯文字阅读大概需要6分钟。
内容图文
![java – spring-boot在单个Web应用程序路径上设置基本身份验证?](/upload/InfoBanner/zyjiaocheng/818/822035e60b8945f9b5ee8e793f0ba2f9.jpg)
我试图在我的spring-boot spring基于MVC的应用程序中设置单个路径(/ basic)以进行基本的auth保护.我只是使用我自己的自定义配置参数来配置它,因此用户名和密码只是“admin”和“admin”.
这当前适用于/ basic路径(我被提示并且可以正确登录).问题是注销不起作用(我不知道为什么),并且还要求其他路径(如/其他所示)提供基本身份验证凭证(在始终被拒绝之前).
static class MyApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/open").permitAll();
http.authorizeRequests().antMatchers("/other").denyAll(); // Block it for now
http.authorizeRequests().antMatchers("/basic").authenticated().and().httpBasic().and().logout().logoutUrl("/basic/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
}
我期望/其他人总是被拒绝,但我不明白为什么基本的auth即将到来. / open按预期工作.我也不明白为什么/ basic / logout不会让我退出(它也不会产生错误信息).我有一个简单的代码作为注销端点的占位符,但如果我没有,那么我得到404.“主页”视图是我的web应用程序根,所以我只想在注销后将用户发送到那里.
@RequestMapping("/logout")
public ModelAndView logout() {
// should be handled by spring security
return new ModelAndView("home");
}
更新:
这是最终似乎有用的解决方案(注销部分除外,仍然无效):
@Configuration
@Order(1) // HIGHEST
public static class OAuthSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/oauth").authorizeRequests().anyRequest().denyAll();
}
}
@Configuration
public static class BasicAuthConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/basic").authorizeRequests().anyRequest().authenticated().and().httpBasic();
http.logout().permitAll().logoutUrl("/logout").logoutSuccessUrl("/").invalidateHttpSession(true);
//.and().logout().logoutUrl("/basic/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
}
解决方法:
我不确定退出,但我们有一些类似的问题,我们的一些网站是基本的,有些不是.我们的解决方案是仅为需要http basic的路径使用第二个嵌套配置类.我们给这个配置一个@Order(1)..但我不确定是否有必要.
更新了代码
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
private static final Logger LOG = LoggerFactory.getLogger(SecurityConfig.class);
@Autowired
public void registerAuthentication(AuthenticationManagerBuilder auth, Config appConfig) throws Exception {
auth.inMemoryAuthentication()
.withUser(appConfig.getString(APIConfig.CONFIG_KEY_MANAGEMENT_USER_NAME))
.password(appConfig.getString(APIConfig.CONFIG_KEY_MANAGEMENT_USER_PASS))
.roles(HyperAPIRoles.DEFAULT, HyperAPIRoles.ADMIN);
}
/**
* Following Multiple HttpSecurity approach:
* http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity
*/
@Configuration
@Order(1)
public static class ManagerEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/management/**").authorizeRequests().anyRequest().hasRole(HyperAPIRoles.ADMIN).and()
.httpBasic();
}
}
/**
* Following Multiple HttpSecurity approach:
* http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity
*/
@Configuration
public static class ResourceEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//fyi: This adds it to the spring security proxy filter chain
.addFilterBefore(createBBAuthenticationFilter(), BasicAuthenticationFilter.class)
;
}
}
}
这似乎使用基本身份验证将执行器端点保护在/ management,而其他人使用自定义身份验证令牌头.我们不会提示凭证(没有发出挑战)但是为了任何事情……我们必须注册其他一些东西来实现这一目标(如果我们想要的话).
希望这可以帮助
内容总结
以上是互联网集市为您收集整理的java – spring-boot在单个Web应用程序路径上设置基本身份验证?全部内容,希望文章能够帮你解决java – spring-boot在单个Web应用程序路径上设置基本身份验证?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。