zoukankan      html  css  js  c++  java
  • Spring Security初识

    Spring Security与Spring Boot集成
    添加依赖:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
    <dependency>
    	<groupId>org.thymeleaf.extras</groupId>
    	<artifactId>thymeleaf-extras-springsecurity4</artifactId>
    	<version>3.0.2.RELEASE</version>
    </dependency>
    
    <!-- h2 database测试时使用的数据库 -->
    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    

    3.1 权限管理

    基于角色的权限管理

    什么是角色?

    • 代表一系列行为或责任的实体
    • 限定了角色能做什么,不能做什么
    • 用户账号往往与角色相关联

    RBAC

    • 基于角色的访问控制(Role-Based Access Control)
    • 隐式访问控制
    if(user.hasRole("Project Manager")){
    	// 显示报表按钮
    } else {
        // 不显示报表按钮
    }
    
    • 显示访问控制(更加灵活)
    if(user.isPermitted("projectReport:view:12345")){
    	// 显示报表按钮
    } else {
        // 不显示报表按钮
    }
    

    权限解决方案框架:

    • Apache Shiro

    • Spring Security

    3.2 Spring Security实战

    1)后台编码

    • 安全配置类
    package com.fei.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    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;
    
    /**
     * 安全配置类 Created by zxf on 2019年10月10日
     */
    @EnableWebSecurity // 启用注解安全
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    	/**
    	 * 自定义拦截策略
    	 */
    	@Override
    	protected void configure(HttpSecurity http) throws Exception {
    
    		http.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll()// 都可以访问
    				.antMatchers("/users/**").hasRole("ADMIN")// 需要相应的角色才能访问
    				.and().formLogin()// 基于form表单登录验证
    				.loginPage("/login").failureUrl("/login-error");// 自定义登录界面
    		super.configure(http);
    	}
    
    	/**
    	 * 认证信息管理
    	 * 
    	 * @param auth
    	 * @throws Exception
    	 */
    	@Autowired
    	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    
    		auth.inMemoryAuthentication()// 认证信息存储在内存中
    				.withUser("fei").password("123").roles("ADMIN");
    	}
    
    }
    
    • 控制器
    package com.fei.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    /**
     * 处理首页、登录
     * Created by zxf on 2019年10月10日
     */
    @Controller
    public class MainController {
    	@GetMapping("/")
    	public String root() {
    		return "redirect:/index";
    	}
    
    	@GetMapping("/index")
    	public String index() {
    		return "index";
    	}
    
    	@GetMapping("/login")
    	public String login() {
    		return "login";
    	}
    
    	@GetMapping("/login-error")
    	public String loginError(Model model) {
    		model.addAttribute("loginError", true);
    		model.addAttribute("errorMsg", "登录失败,用户名或密码错误!");
    		
    		return "login";
    	}
    }
    
  • 相关阅读:
    Oracle EBS-SQL (INV-1):库存货位列表.sql
    Oracle EBS-SQL (SYS-24):职责列表
    Oracle EBS-SQL (SYS-23):用户权限查询.sql
    Oracle EBS-SQL (SYS-22):sysadmin_用户职责查询.sql
    Oracle EBS-SQL (SYS-21):sys_用户名与人员对应关系查询.sql
    Oracle EBS-SQL (SYS-20):职责使用菜单2.sql
    Oracle EBS-SQL (SYS-20):OPM接口处理.sql
    Oracle EBS-SQL (SYS-19):sys-用户登陆纪录查询.sql
    Oracle EBS-SQL (SYS-18):检查系统安装的各个表是否打开(PJM%).sql
    Oracle EBS-SQL (SYS-17):查询一张报表在哪个职责下面.sql
  • 原文地址:https://www.cnblogs.com/zxfei/p/11651081.html
Copyright © 2011-2022 走看看