弹簧启动弹簧安全
问题描述:
我正在尝试创建一个弹簧启动REST应用程序。当我部署我的应用程序时,它需要身份验证,并要求输入用户名和密码。我怎样才能绕过这个或如何添加用户名和密码进行身份验证?弹簧启动弹簧安全
我需要删除pom中的安全条目吗?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
答
无需从pom.xml
中删除安全性。在你的项目中,你可以尝试下面的东西。尝试创建SecurityConfig
,它将扩展WebSecurityConfigurerAdapter
并提供一些用户名和密码,稍后您可以对其进行自定义。
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("user")
.roles("USER")
.and()
.withUser("user2")
.password("secret2")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated();
http
.httpBasic();
http
.csrf().disable();
}
}
public @interface EnableWebMvcSecurity {
}
答
如果你不想使用验证可言,你应该删除的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
如果春季安全在类路径上,那么web应用程序默认是安全的所有HTTP端点上的'基本'认证。