Spring Boot的基本身份验证

agu*_*ina 3 basic-authentication spring-boot

我正在开发一个带有spring-boot和angularjs的小应用程序.这个想法是应用程序的后端公开了一些服务,而前端使用这些服务.我正在尝试设置基本身份验证

这是我的pom.xml

<!-- Web Server -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- End Web Server -->

<!-- Spring Security -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- End Spring Security -->
Run Code Online (Sandbox Code Playgroud)

两者都是1.1.1.RELEASE版本.我的WebSecurityConfig

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**", "/js/**").permitAll()
                .anyRequest().authenticated();
        http
                .formLogin()
                .defaultSuccessUrl("/")
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.似乎用户和密码未在内存中设置.

当spring boot运行时,它会创建一个默认密码,这是控制台中出现的密码

AuthenticationManagerConfiguration : 

Using default security password: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

该应用程序使用该密码.

谢谢

Cod*_*und 6

您可以通过在application.properties文件中设置以下属性来覆盖默认用户名(默认值=用户)和动态生成的密码:

security.user.name=user # Default user name.
security.user.password= # Password for the default user name. A random password is logged on startup by default.
Run Code Online (Sandbox Code Playgroud)

要注册多个用户,您需要构建我们自己的AuthenticationManager配置.