【问题标题】:Secure an single page application with spring使用 spring 保护单页应用程序
【发布时间】:2015-08-20 18:30:43
【问题描述】:
我在 js 中创建了一个单页应用程序,我还使用了一些 jquery 命令和 twitter 引导程序。
我以这种方式向我的页面收费
$('#contact').click(function () {
$('#main').load('contact.html');
});
我在服务器上使用 spring java 和一个完整的架构。
有没有一种简单的方法可以使用这些框架来保护我的网页?
【问题讨论】:
标签:
javascript
spring
spring-security
【解决方案1】:
我认为对您来说最好的方法是添加 spring 安全依赖项,您将获得对服务 REST 的完全控制以及与 OAuth、Social(Facebook、Twitter ...)等多个模块的集成.
使用 Spring Security,您可以配置配置 Java 类或 XML 的权限
享受样品:
@配置
@EnableWebSecurity
@Import({ConfigDAO.class, ConfigService.class})
公共类 WebSecurityConfig 扩展 WebSecurityConfigurerAdapter {
@Autowired
private DataSource datasource;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(datasource)
.passwordEncoder(passwordEncoder)
.usersByUsernameQuery("select usuario, senha as password, habilitado as enabled from cds_usuario where usuario = ? ")
.authoritiesByUsernameQuery("select usuario, perfil as authority from cds_usuario where usuario = ?")
.getUserDetailsService();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/painel**").access("hasRole('ROLE_ALUNO')")
.antMatchers("/").access("permitAll")
.antMatchers("/cadastro**").access("permitAll")
.antMatchers("/error/**").access("permitAll")
.and().formLogin().usernameParameter("username").passwordParameter("senha")
.loginPage("/").loginProcessingUrl("/autenticar")
.failureUrl("/")
.defaultSuccessUrl("/painel")
.and().logout().deleteCookies("remove")
.invalidateHttpSession(false)
.logoutUrl("/logout").logoutSuccessUrl("/")
.and().csrf().disable()
.exceptionHandling().accessDeniedPage("/403");
http.sessionManagement().maximumSessions(1).expiredUrl("/logout");
}
}