繁体   English   中英

Spring Boot + MVC(Rest)+ angularJS:增加安全性

[英]Spring boot + mvc (rest) + angularJS: add security

我有一个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() ,我可以设置用户名和密码来访问受保护的URL,但是如果只有一个用户,该怎么办? 我的意思是,我想向数据库中注册的所有用户授予访问权限。

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

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

您有很多选择,所有这些都需要很多代码,但是大多数选项已经在Internet上很好地实现了,因此您只需要使其适合您的需求即可。 您可以采用最简单的方法,例如http会话,JSON令牌或JWT(JSON Web令牌)或其他方法。

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

在这个baeldung网站 (即使它使用XML配置)和这个网站 (使用Java配置)上找到了很大的帮助。

要全面了解Spring安全性是如何工作的, 此链接对我有很大帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM