【问题标题】:Spring boot + mvc (rest) + angularJS: add securitySpring boot + mvc (rest) + angularJS:添加安全性
【发布时间】:2015-09-01 00:52:58
【问题描述】:

我有一个前后分开的 Web 应用程序:

/project  
+--- /back
+--- /front

背面使用Spring boot + Spring MVC开发,正面使用AngularJS开发。

我正在尝试为后/前之间的通信设置安全性。我做了什么:

- create a ConfigSecurity class which extends from WebSecurityConfigurerAdapter
- create a SpringWebMvcInitializer which extends from AbstractAnnotationConfigDispatcherServletInitializer and call ConfigSecurity
- create a SecurityWebInitializer class which extends AbstractSecurityWebApplicationInitializer

我的 ConfigSecurity 如下所示:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ConfigSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/demo/**","/home").permitAll()
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin().loginPage("...")
                .and()
                .httpBasic();
    }
}

我的问题:

  1. configureGlobal() 中,我可以设置用户名和密码来访问我受保护的网址,但如果不是只有一个用户,我该怎么办?我的意思是,我想授予在我的数据库中注册的所有用户的访问权限。

  2. 既然后端和前端只是和REST通信(通过JSON文件),那么ConfigSecurity中的formLogin()应该怎么做呢?默认情况下,Spring Security 会生成一个默认登录表单。我不需要它在应用程序的后面,因为它是负责显示 loginPage 的前面。如何跳过后面的登录页面?也许通过将用户名和密码放在前面发送到后面的 JSON 文件中?有人知道怎么做吗?

我正在为 Spring 使用 Java 配置(不是 XML 配置)。

【问题讨论】:

标签: angularjs spring spring-mvc spring-security spring-boot


【解决方案1】:

baeldung website(即使它使用 XML 配置)和 this website(使用 Java 配置)上找到了很大的帮助。

对于 Spring 安全性如何工作的全局解释,this link 对我有很大帮助。

【讨论】:

    【解决方案2】:

    您有很多选择,所有这些都需要相当多的代码,但其中大部分已经在互联网上很好地实现了,所以您只需要使其满足您的需求即可。您可以选择最简单的方式,例如 http 会话、JSON 令牌或 JWT(JSON Web 令牌)或其他任何方式。

    HTTP Sessions 肯定是最容易设置的,并且开箱即用的 Spring Security 已经很好地支持了。

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2017-03-13
      • 2014-07-19
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2023-01-15
      相关资源
      最近更新 更多