相关疑难解决方法(0)

用于Web的RESTFul和FormLogin(Cookies)的Spring Security HTTP Basic - 注释

在具体

我想只为特定的URL模式进行HTTP基本身份验证.

详细地

我正在为我的应用程序创建一个API接口,需要通过简单的HTTP基本身份验证进行身份验证.但是其他网页应该使用HTTP basic,而应该使用普通的表单登录.

当前配置 - 不工作

@Override
protected void configure(HttpSecurity http) throws Exception {
    http //HTTP Security
            .csrf().disable() //Disable CSRF
            .authorizeRequests() //Authorize Request Configuration
                .antMatchers("/connect/**").permitAll()
                .antMatchers("/", "/register").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/api/**").hasRole("API")
                .anyRequest().authenticated()
            .and() //HTTP basic Authentication only for API
                .antMatcher("/api/**").httpBasic()
           .and() //Login Form configuration for all others
                .formLogin().loginPage("/login").permitAll()
            .and() //Logout Form configuration
                .logout().permitAll();

}
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security spring-boot

34
推荐指数
1
解决办法
3万
查看次数

Spring-Boot REST 服务基本 http auth 排除一个端点

我有一个基于 Spring-Boot 版本 1.5.4.RELEASE 的 REST-only 微服务,带有 spring-boot-starter-security。该服务没有网页,只有 JSON 输入和输出。用户名和密码在 application.properties 文件中配置。归功于http://ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/,以下配置使服务器很好地实现了基本的 HTTP 身份验证,它接受凭据并拒绝未经授权的要求:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests() //
                .anyRequest().authenticated().and().httpBasic();        
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我想从基本的 HTTP 身份验证中排除一个小的 ole 路径,一个小的端点。从疯狂的谷歌搜索和复制粘贴我修改了上面的内容:

    http.csrf().disable().authorizeRequests() //
        .antMatchers("/healthcheck").permitAll()
        .anyRequest().authenticated().and().httpBasic();
Run Code Online (Sandbox Code Playgroud)

这会在没有警告的情况下编译和运行,但不会为未经身份验证的访问打开该路径。我仍然必须提供凭据来检查服务运行状况。

我必须一一匹配路径吗?我的小健康检查端点位于上下文路径的基础上,还有一大堆其他端点 - 逐个添加路径会很麻烦。

我的 application.properties 文件的相关部分是:

security.user.name = web-user
security.user.password = web-pass
management.security.roles=SUPERUSER
Run Code Online (Sandbox Code Playgroud)

也许我需要以某种方式摆弄角色?

请帮忙,提前致谢。

更新 1:

路径信息 - 我希望这条路径(以及更多根路径)受到保护:

localhost:8081/abcd/user
Run Code Online (Sandbox Code Playgroud)

而且我只希望打开这一条路径,无需身份验证:

localhost:8081/abcd/healthcheck
Run Code Online (Sandbox Code Playgroud)

更新 2:看起来我在很大程度上重复了这个 3 年前的问题,但没有为我的问题接受答案:

spring-boot …

java rest spring-boot

6
推荐指数
1
解决办法
9884
查看次数

标签 统计

java ×2

spring-boot ×2

rest ×1

spring ×1

spring-mvc ×1

spring-security ×1