java - Spring boot中如何实现基本认证?

标签 java angularjs spring-security spring-boot

我关注了thisthis创建具有基于 Java 后端的 Angular.JS 应用程序框架的教程。其代码可参见 here .

它具有身份验证功能 - 在启动时 Spring boot 会向控制台写入一个随 secret 码,您可以使用该密码登录已启动的应用程序。用户名是user,密码在开始时打印在控制台中:

Console output

现在我想要几个具有固定密码的用户帐户(如果它们是硬编码的,那就没问题)。

如何在该应用程序中执行此操作而不破坏与 AngularJS 的兼容性?

我想我必须修改UiApplication中的SecurityConfiguration并使用类似的东西

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("user")
        .password("password")
        .roles("USER");
}

如所解释的here ,但我不确定它不会破坏 AngularJS。

最佳答案

这就是解决方案:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
class SecurityConfiguration extends
    WebSecurityConfigurerAdapter {


    @Autowired
    public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user1")
            .password("password1")
            .roles("ADMIN")
            .and()
            .withUser("user2")
            .password("password2")
            .roles("USER");
    }

关于java - Spring boot中如何实现基本认证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153357/

相关文章:

javascript - 如何在 Rails 应用程序中使用 Bower 包含修改后的 javascript 库?

javascript - 托管后 Google map 未在 Angular View 中呈现

java - 对于大型 ByteBuffer,单独 SocketChannel 的并发 read() 速度较慢

java - 快速排序方法有问题

javascript - 如何获取 ng-options 值并在内部值发生变化时触发函数?

spring-security - Spring 3 : Can't find AuthenticationProcessingFilter

java - Spring 拦截 url 配置中的 ROLE_USER 和 ROLE_ANONYMOUS 有什么区别?

grails - 如何禁止在grails中访问 View 或 Controller

java - 奇怪的字节 ArrayList 重写其值

java - 在java中获取远程tcp连接的ip地址?