java - Spring Boot和http认证的简单应用
问题描述
我刚刚开始学习 Web 编程,并且正在尝试使用 Spring Boot 编写我的第一个应用程序,它为 HRM 系统提供了一个 RESTful API。但是我在 HTTP 身份验证方面遇到了困难。我尝试了很多指南,但他们没有帮助。
如果没有安全设置,RESTful API 可以工作,但是如果我为我的 API 包含安全性,我会得到代码 401。我该如何修复它?
Screenshot_from_REST_client_1
Screenshot_from_REST_client_2
Screenshot_from_REST_client_3
Screenshot_from_REST_client_4
SpringSecurityConfig.java(无包和导入)
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AccessDeniedHandler accessDeniedHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers().permitAll()
.antMatchers("/**").hasAnyRole("USER")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic()
.and()
.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
PS 英语不是我的母语,所以请善待我的错误。我真的在努力提高我的英语。
解决方案
推荐阅读
- r - 如何为一个 (Y) 轴生成 ggplot Log10 比例图?
- c# - 谁能告诉我为什么 AllowDrop 不适用于文本框
- amazon-web-services - Cloudfront 证书和主机名:它们可以不同吗?
- node.js - 多个快速的外部 POST 请求
- javascript - 如何对同一动作类中的方法调用动作?
- micronaut - Picocli 命令中的注入与 micronaut 之间的互操作性
- docker - 如何在单个 AWS EC2 实例上设置 3 节点 Elasticsearch 集群?
- enums - 用其他值替换地图中的空值
- android - Android:获取芯片图标点击事件
- oracle - 权限缺失 - Oracle System 事件触发问题 12c