关于shiro:如何控制项目的访问权限来保证项目安全SpringBoot-集成-Spring-Security-安全框架使用指南

平安框架

  • shiro
  • Spring Security
  • 应用程序的两个次要区域:认证受权(这两个次要区域是Spring Security的两个指标)

    • 认证(Authentication):

      • 建设一个申明的主体过程
      • 一个[主体]个别是指[用户],[设施]或一些能够[在应用程序中执行动作的其它零碎]
    • 受权(Authorization):

      • 访问控制确定一个主体是否容许在你的应用程序执行一个动作的过程
      • 为了到达须要受权的点,主体身份曾经有认证过程的建设

        Spring Security

  • 针对Spring我的项目的平安框架,是Spring Boot底层平安模块默认的技术选型
  • 能够实现web安全控制,只须要引入spring-boot-starter-security依赖配置即可
  • Spring Security中的类:

    • WebSecurityConfigurerAdapter: 自定义Security策略
    • AuthenticationManagerBuilder: 自定义认证策略
    • @EnableWebSecurity: 开启WebSecurity模式

      1.引入spring-boot-starter-security依赖
      
      2.编写SpringSecurity配置类
      2.1 定制申请的受权规定
      2.2 开启主动配置的登录性能(/login来到登录页;重庆向到/login?error示意登录失败)
      2.3 开启主动配置的登记性能(拜访/logout申请,示意用户登记并清空session;登记胜利返回/login?logout)
      2.4 开启主动配置的记住明码性能(http.rememberMe();)-登录胜利当前,将Cookie发送给浏览器保留,能够实现记住明码性能;点击登记会删除Cookie,就没有记住明码性能
      默认post模式的/login代表解决登录
      2.5定义认证规定
      @EnableSecurity
      public class MySecurityConfig extends WebSecurityConfigureAdapter{
      @Override
      protected void configure(HttpSecurity http) throws Exception{
          // 定制申请的受权规定
          http.authorizeRequest().antMatches("/").permitAll()
              .antMatches("/level/**").hasRole("VIP");
          // 开启主动配置的登录性能,如果没有权限就会跳转到登录页面
          http.formLogin().loginPage("/");    // 跳转到自定义登录页
          http.logout().logoutSuccessUrl("/");    // 登记胜利返回首页
          http.rememberMe().rememberMeParameter("remember");    // 开启主动配置的记住明码性能
      }
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception{
          auth.inMemoryAuthentication().withUser("username").password("password").roles("role1","role2")
                                       .and()
                                       .withUser("username").password("password").roles("role1","role2")
      }
      
      
      3.管制申请的拜访权限
  • Thymeleaf Extras Springsecurity4

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理