【问题标题】:How to implement basic authentication in Spring boot?如何在 Spring Boot 中实现基本身份验证?
【发布时间】:2015-09-18 03:22:09
【问题描述】:

我按照 thisthis 教程创建了一个带有基于 Java 的后端的 Angular.JS 应用程序的骨架。它的代码可以在here找到。

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

现在我想拥有几个具有固定密码的用户帐户(没关系,如果它们是硬编码的)。

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

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

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

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

【问题讨论】:

    标签: java angularjs spring-security spring-boot


    【解决方案1】:

    这就是解决方案:

    @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");
        }
    

    【讨论】:

      【解决方案2】:

      只要您不更改身份验证方案(即 BASIC、FORM 等),AngularJS 就不会关心您如何在后端管理用户帐户。当然,如果您使用固定的用户名和密码并将其硬编码到 AngularJS 代码中,则必须更改此设置。

      【讨论】:

        【解决方案3】:

        它不会破坏角度兼容性。但除了您提到的代码更改之外,您还需要确保您允许匿名访问 /login URI。还需要更改重载的配置方法,如下所示:

        @Override
        protected void configure(HttpSecurity http) throws Exception {
          http
            .httpBasic()
          .and()
            .authorizeRequests()
              .antMatchers("/login.html").permitAll()
          .antMatchers("/index.html", "/home.html").authenticated()
              .antMatchers("/index.html", "/home.html").hasRole("USER")
              .anyRequest().authenticated();
        }
        

        【讨论】:

          猜你喜欢
          • 2017-11-02
          • 2019-04-25
          • 2018-04-13
          • 1970-01-01
          • 1970-01-01
          • 2019-05-29
          • 2017-07-31
          • 2018-10-27
          • 2011-12-06
          相关资源
          最近更新 更多