spring安全框架
身份验证机制很多(基本,摘要,形式,X.509等),凭据和权限信息(内存中,数据库,LDAP等)的存储选项也很多。 授权取决于身份验证,并确定您是否具有所需的权限。 决策过程通常基于角色(例如ADMIN,MEMBER,GUEST等)。
在Web环境中,可以通过三个步骤来设置和配置Spring Security:
- 设置过滤器链:实现是由Spring配置的过滤器链(Spring Boot自动执行)
- 配置安全(授权)规则
- 设置Web身份验证
在下一个示例中,它被定义为使用mvcMatchers的URL的特定授权限制。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login").permitAll().and().exceptionHandling().accessDeniedPage("/denied").and()
.authorizeRequests().mvcMatchers("/accounts/resources/**").permitAll().mvcMatchers("/accounts/edit*")
.hasRole("EDITOR").mvcMatchers("/accounts/account*").hasAnyRole("VIEWER", "EDITOR")
.mvcMatchers("/accounts/**").authenticated().and().logout().permitAll().logoutSuccessUrl("/");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new StandardPasswordEncoder()).withUser("viewerUser")
.password("abc").roles("VIEWER").and().withUser("editorUser").password("abc").roles("EDITOR");
}
}
如您所见,您可以链接多个限制(使用and()方法)。 第一种方法设置基于表单的身份验证。 第二种方法使用UserDetailsManagerConfigurer。 您可以使用jdbcAuthentication()代替inMemoryAuthentication()。
在Spring Security Reference上了解更多信息
翻译自: https://www.javacodegeeks.com/2020/12/spring-security.html
spring安全框架