栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringSecurity

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringSecurity

 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

我们使用的还是springboot的环境,所以还是先建立一个springboot项目,只需要勾选spring web与thymeleaf 模块。

一、导入依赖
        
            org.springframework.boot
            spring-boot-starter-security
        
二、自定义Config

自定义一个Config配置类,我起名为SecurityConfig。继承WebSecurityConfigurerAdapter 并加上@EnableWebSecurity注解,表明开启了Security,交给springboot托管。

package com.xyh.Config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    
}

需要继承两个类  configure(HttpSecurity http) (授权) 与 configure(AuthenticationManagerBuilder auth) (认证)

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人都可以访问,功能页只有对应的人才可以访问
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")//每个等级只能到对应的level中
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限,会跳转到登录界面,需要开启登录界面(Security自带的login页面,若没有登录则跳抓到登录页面,这条代码就是表示开启登录界面)
        http.formLogin();
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())//需要密码的加密
                .withUser("xss").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")//注册每一个用户的账号,密码,以及权限
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
    }
 注销用户

    spring Security 也帮我们封装了一个方法,我们直接调用就可以

        //注销
        http.logout().logoutSuccessUrl("/");

  logoutSuccessUrl("/")的作用是注销后跳转到哪个界面

三、thymeleaf集成spring Security

  有时候,在前端我们想要让用户只能看到自己权限之内的东西,之前用th:if来判断,但现在我们可以集成spring Security来更好的实现这一目的

   一、导入依赖
        
            org.thymeleaf.extras
            thymeleaf-extras-springsecurity5
            3.0.4.RELEASE
        

  注意:新版的springboot需要的是thymeleaf-extras-springsecurity5,导入其他版本的可能会无法使用

  二、添加约束

   在要使用的前端页面的头部添加约束

 
三、使用 

 我们现在的想法是让如果没有登录,显示登录二字,如果已经登录,则显示登陆人的用户名,权限以及注销二字

 
                
                
                    
                         登录
                    
                
                
                
                    
                        用户名:
                        角色:
                    
                    
                         注销
                    
                

  想让权限vip1只能看到vip1的level,则判断Role里是否有“vip1”

                
                    
                        
                            
Level 1
Level-1-1 Level-1-2 Level-1-3

 登录中经常有“记住我”这个选项,所以springsecurity也存在这个配置

 在configure(HttpSecurity http)中配置

        //开启记住我功能 保存cookie,默认保存2周
        http.rememberMe().rememberMeParameter("remember");

则表示开启了“记住我”功能,后面的rememberMeParameter("remember");表示在前端页面上组件的name为“remember”。

上面说过当我们没有登录时,开启http.formLogin(),springsecurity会自动跳到自己内部就有的login页面,当然我们也可以自己写一个登录界面,很简单,就是在http.formLogin()后面添加一个链式模块

        //没有权限,会跳转到登录界面,需要开启登录界面
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/lll");

loginPage表示我们自己的登录页面的链接,loginProcessingUrl表示前端页面要登录时所提交的路径。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345245.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号