spring安全框架_Spring安全

spring安全框架

spring安全框架

身份验证机制很多(基本,摘要,形式,X.509等),凭据和权限信息(内存中,数据库,LDAP等)的存储选项也很多。 授权取决于身份验证,并确定您是否具有所需的权限。 决策过程通常基于角色(例如ADMIN,MEMBER,GUEST等)。

在Web环境中,可以通过三个步骤来设置和配置Spring Security:

  1. 设置过滤器链:实现是由Spring配置的过滤器链(Spring Boot自动执行)
  2. 配置安全(授权)规则
  3. 设置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安全框架

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值