SpringSecurity权限控制

权限管理过程中的相关概念

2.1 主体

英文单词:principal
使用系统的用户或设备或从其他系统远程登录的用户等等。简单说就是谁使用系统
谁就是主体。

2.2 认证

英文单词:authentication
权限管理系统确认一个主体的身份,允许主体进入系统。简单说就是“主体”证明
自己是谁。
笼统的认为就是以前所做的登录操作。

2.3 授权

英文单词:authorizatio
将操作系统的“权力”“授予”“主体”,这样主体就具备了操作系统中特定功能的
能力。
所以简单来说,授权就是给用户分配权限

整个流程在这里插入图片描述

配置

1.web.xml 配置拦截器

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.添加配置类(继承WebSecurityConfigurerAdapter)
在这里插入图片描述
3.配置完成后 默认拦截所有资源。
在这里插入图片描述

实验一 登录 并在内存中模拟验证

    <form action="${pageContext.request.contextPath }/doLogin.html" method="post">
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        <div class="layadmin-user-login-main">
    </form>
// 当前类必须在mvc的包扫描下
@Configuration
// 启用web环境下权限控制功能
@EnableWebSecurity
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication() // 在内存中完成账号、密码的检查
                .withUser("tom") // 指定账号
                .password("123123") // 指定密码
                .roles("ADMIN", "学徒") // 指定当前用户的角色
                .and()
                .withUser("jack") // 指定账号
                .password("123123") // 指定密码
                .roles("大师") // 指定当前用户角色
        // .authorities("UPDATE", "内门弟子") // 指定当前用户的权限
        ;

    }

    // 重写方法 自定义拦截规则
    @Override
    protected void configure(HttpSecurity security) throws Exception {
        security.authorizeRequests()                    // 针对请求进行授权
                .antMatchers("/index.jsp")  // 针对/index.jsp路径进行授权
                .permitAll()                            //  可以无条件访问
                .antMatchers("/layui/**").permitAll()// 放行静态资源
                .antMatchers("/level2/**").hasRole("学徒") // 角色
                .antMatchers("/level3/**").hasAnyAuthority("大师") // 权限
                .anyRequest()                           // 其他的任意请求
                .authenticated()                        // 需要授权才能访问
                /**
                 * 关于loginPage()方法的特殊说明
                 * 指定登录页的同时会影响到:“提交表登录表单的地址” ,“退出登录地址” ,"登录失败地址"
                 * /index GET - the login form
                 * /index POST - process the credentials and if valid authenticate the user  // 这个登录表单的地址
                 * /index?error GET - redirect here for failed authentication attempts // 登录失败
                 * /index?logout GET - redirect here after successfully logging out // 退出登录
                 */
                .and().formLogin().loginPage("/index.jsp") // 未登录时跳转到指定登录页面
                .loginProcessingUrl("/doLogin.html").permitAll() // 另外指定提交登录表单的地址
                .usernameParameter("loginAcct") // 修改账号的name值,默认为“username”
                .passwordParameter("userPwd") // 修改密码的name值,默认为“password”
                .defaultSuccessUrl("/main.html") // 指定登录成功后地址
                //
                .and()                       // 如果没有禁用 CSRF:请求必须携带 CSRF 的 token 值。如果已经禁用 CSRF:没有限制。
                .csrf().disable()            // 禁用CSRF功能
                .logout().logoutUrl("/doLoginOut.html").logoutSuccessUrl("/index.jsp") // 退出
                .and()

                // // 方案一:
                // .exceptionHandling()  // 指定异常处理器
                // .accessDeniedPage("/to/no/auth/page.html"); // 访问被拒绝时前往的页面

                // 方案二:
                .exceptionHandling()
                .accessDeniedHandler((request, response, accessDeniedException) -> {
                    request.setAttribute("message", "抱歉,这个资源您无法访问!");
                    request.getRequestDispatcher("/WEB-INF/views/no_auth.jsp").forward(request, response);
                })
        ;

    }
}

退出登录时。如果没有禁用 CSRF:请求必须携带 CSRF 的 token 值。如果已经禁用 CSRF:没有限制。
注意:SpringSecurity 会在底层用“ROLE_”区分角色和权限。角色信息会被附加 “ROLE_”前缀

“记住我” 功能实现(内存版)

在这里插入图片描述

调用 security.rememberMe()方法开启记住我功能。
表单中提供名为 remember-me 的请求参数。为了用户便于操作,通常会使用多选
框。

<input type="checkbox" name="remember-me" lay-skin="primary" title="记住我"/

原理分析:

相关依赖

        <!--spring security 对web应用进行权限管理-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.2.10.RELEASE</version>
        </dependency>

        <!--spring security 配置-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.2.10.RELEASE</version>
        </dependency>

        <!--spring security 标签库-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>4.2.10.RELEASE</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值