带有Spring Boot的Spring Security

溜冰场

我正在尝试创建一个Spring Boot REST应用程序。部署应用程序时,需要进行身份验证,并且要求我提供用户名和密码。如何绕过此操作或如何添加用户名和密码进行身份验证?

我需要删除pom中的安全性条目吗?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Subhasish萨胡岛

无需从删除安全性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 {

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有Spring Boot和OAuth 2的Spring Security

带有Spring Security的Recaptcha

带有POST请求的Spring Boot Security CORS

Spring Boot Security CORS

带有REST API的Spring Security

带有oidc的Spring Security:刷新令牌

Spring Security @PreAuthorize带有AuthenticationSuccessEvent的注释

带有主干URL的Spring Security

Spring Security UserNotFoundException在带有JSP视图模板的Spring Boot中不显示错误消息

带有spring-boot的spring-security,自定义登录页面,错误403

Spring Boot Security匿名授权

Spring Boot Security上的NoSuchMethodError

带有Spring Security的Java Spring冲突依赖版本

Spring Boot或Spring Security内存可能泄漏

Spring Boot中Spring Security的XML配置

什么是Spring Boot的Spring Security默认凭据?

Spring Boot 中 Spring Security 的困惑

没有Spring Security的Spring-Boot登录

带有ZooKeeper的Spring Boot Security 4-安全集成问题

带有自定义提供程序的 Spring Boot + Security OAuth2.0 客户端

带有Spring Boot的Spring Security:将基本身份验证与JWT令牌身份验证混合

Spring Security-仅允许带有前缀的请求

带有REST服务的Spring Integration Security示例

带有AngularJS的Spring Security-注销不起作用

Spring Security和带有匿名用户的RequestCache

带有@EnableGlobalMethodSecurity的Spring Security AspectJMode不起作用

带有 Spring Security 的 JWT 基于角色的授权

Spring Security中带有查询参数的URL的regexMatcher

带有多个路径的Spring Security http antMatcher