SpringBoot 集成Spring Security
1、pom文件导入starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、参考文档
https://docs.spring.io/spring-security/site/docs/current/guides/html5/
3、Hello Spring Security with Boot
编写Spring Security的配置类
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
}
默认登录页面/login
登录失败页面/login?error
注意的问题
1)出现这个问题的原因是因为Spring Security默认将header response里的X-Frame-Options属性设置为DENY。
如果页面里有需要通过iframe/frame引用的页面,需要配置Spring Security允许iframe frame加载同源的资源,
方法为在Spring Security的配置类里将header response的X-Frame-Options属性设置为SAMEORIGIN,具体可以参考如下代码:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/res/**", "/admin", "/thirdparty/**", "/auth/login").permitAll()
.antMatchers("/admin/**").hasAuthority("admin:index")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/admin").permitAll()
.and()
.logout().logoutUrl("/admin/logout").logoutSuccessUrl("/admin").invalidateHttpSession(true)
.and()
.csrf().disable()
.headers().frameOptions().sameOrigin();
}