zoukankan      html  css  js  c++  java
  • Day03_企业权限管理(SSM整合)

    学于黑马程序员和传智播客联合做的教学项目 感谢
    黑马程序员官网
    传智播客官网
    个人根据教程的每天的工作进度的代码和资料 密码:cti5
    b站在线视频
    微信搜索"艺术行者",关注并回复关键词"企业权限管理"获取视频和教程资料!

    第三天

    SSM权限操作

    • 创建数据库
    • 创建用户信息表
    -- 用户表
    CREATE TABLE users(
    id varchar2(32) default SYS_GUID() PRIMARY KEY,
    email VARCHAR2(50) UNIQUE NOT NULL,
    username VARCHAR2(50),
    PASSWORD VARCHAR2(50),
    phoneNum VARCHAR2(20),
    STATUS INT
    )
    
    • 编写实体类
    package org.hacker.ssm.domain;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:09
     */
    public class UserInfo {
        private String id;
        private String username;
        private String email;
        private String password;
        private String phoneNum;
        private int status;
        private String statusStr;
        private List<Role> roles;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getPhoneNum() {
            return phoneNum;
        }
    
        public void setPhoneNum(String phoneNum) {
            this.phoneNum = phoneNum;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        public String getStatusStr() {
            return statusStr;
        }
    
        public void setStatusStr(String statusStr) {
            this.statusStr = statusStr;
        }
    
        public List<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(List<Role> roles) {
            this.roles = roles;
        }
    }
    
    • 创建角色表
    CREATE TABLE role(
    id varchar2(32) default SYS_GUID() PRIMARY KEY,
    roleName VARCHAR2(50),
    roleDescVARCHAR2(50)
    )
    
    • 编写实体类
    package org.hacker.ssm.domain;
    
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:12
     */
    public class Role {
        private String id;
        private String roleName;
        private String roleDesc;
        private List<Permission> permissions;
        private List<UserInfo> users;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getRoleName() {
            return roleName;
        }
    
        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
    
        public String getRoleDesc() {
            return roleDesc;
        }
    
        public void setRoleDesc(String roleDesc) {
            this.roleDesc = roleDesc;
        }
    
        public List<Permission> getPermissions() {
            return permissions;
        }
    
        public void setPermissions(List<Permission> permissions) {
            this.permissions = permissions;
        }
    
        public List<User> getUsers() {
            return users;
        }
    
        public void setUsers(List<User> users) {
            this.users = users;
        }
    }
    
    • 创建角色-用户中间表
    -- 用户角色关联表
    CREATE TABLE users_role(
    userId varchar2(32),
    roleId varchar2(32),
    PRIMARY KEY(userId,roleId),
    FOREIGN KEY (userId) REFERENCES users(id),
    FOREIGN KEY (roleId) REFERENCES role(id)
    )
    
    • 创建资源表
    CREATE TABLE permission(
    id varchar2(32) default SYS_GUID() PRIMARY KEY,
    permissionName VARCHAR2(50),
    url VARCHAR2(50)
    )
    
    • 编写实体类
    package org.hacker.ssm.domain;
    
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:17
     */
    public class Permission {
        private String id;
        private String permissionName;
        private String url;
        private List<Role> roles;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getPermissionName() {
            return permissionName;
        }
    
        public void setPermissionName(String permissionName) {
            this.permissionName = permissionName;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public List<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(List<Role> roles) {
            this.roles = roles;
        }
    }
    
    • 创建权限-角色表
    -- 角色权限关联表
    CREATE TABLE role_permission(
    permissionId varchar2(32),
    roleId varchar2(32),
    PRIMARY KEY(permissionId,roleId),
    FOREIGN KEY (permissionId) REFERENCES permission(id),
    FOREIGN KEY (roleId) REFERENCES role(id)
    )
    

    Spring Security概述

    Spring Security 是 Spring 项目组中用来提供安全认证服务的框架。Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业软件项目。

    • 如何使用
    1. 导入依赖到总工程的pom文件中
    <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>5.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>5.0.1.RELEASE</version>
    </dependency>
    
    1. 配置web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <!-- 配置加载类路径的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext.xml</param-value>
        </context-param>
    
        <!-- 配置监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
    <!--    spring security-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-security.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <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>
    
                <!-- 解决中文乱码过滤器 -->
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    
    • 项目中web.xml完整代码
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <!-- 配置加载类路径的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
        </context-param>
    
        <!-- 配置监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
    
        <!-- 解决中文乱码过滤器 -->
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    
    • 编写spring-security配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:security="http://www.springframework.org/schema/security"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">
    
        <!-- 配置不拦截的资源 -->
        <security:http pattern="/login.jsp" security="none"/>
        <security:http pattern="/failer.jsp" security="none"/>
        <security:http pattern="/css/**" security="none"/>
        <security:http pattern="/img/**" security="none"/>
        <security:http pattern="/plugins/**" security="none"/>
        <!--
            配置具体的规则
            auto-config="true"    不用自己编写登录的页面,框架提供默认登录页面
            use-expressions="false"    是否使用SPEL表达式(没学习过)
        -->
        <security:http auto-config="true" use-expressions="false">
            <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
            <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>
    
            <!-- 定义跳转的具体的页面 -->
            <security:form-login
                    login-page="/login.jsp"
                    login-processing-url="/login.do"
                    default-target-url="/index.jsp"
                    authentication-failure-url="/failer.jsp"
                    authentication-success-forward-url="/pages/main.jsp"
            />
    
            <!-- 关闭跨域请求 -->
            <security:csrf disabled="true"/>
            <!-- 退出 -->
            <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />
    
        </security:http>
    
        <!-- 切换成数据库中的用户名和密码 -->
        <security:authentication-manager>
            <security:authentication-provider user-service-ref="userService">
                <!-- 配置加密的方式
                <security:password-encoder ref="passwordEncoder"/>-->
            </security:authentication-provider>
        </security:authentication-manager>
    
        <!-- 配置加密类 -->
        <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    </beans>
    
    • 导入登录和登陆失败页面
    • 编写service接口及其实现类
    • 编写dao接口
    package org.hacker.ssm.service;
    
    import org.springframework.security.core.userdetails.UserDetailsService;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:34
     */
    public interface IUserService extends UserDetailsService {
    }
    
    package org.hacker.ssm.service.impl;
    
    import org.hacker.ssm.domain.Role;
    import org.hacker.ssm.domain.UserInfo;
    import org.hacker.ssm.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:34
     */
    @Service("userService")
    @Transactional
    public class UserServiceImpl implements IUserService {
    
        @Autowired
        private IUserDao userDao;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UserInfo userInfo = userDao.findByUsername(username);
            List<Role> roles = userInfo.getRoles();
            //处理自己的用户对象封装成UserDetails
            User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(), userInfo.getStatus() == 0 ? false : true, true, true, true, getAuthority(userInfo.getRoles()));
            return user;
        }
    
        //作用就是返回一个List集合,集合中装入的是角色描述
        public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
    
            List<SimpleGrantedAuthority> list = new ArrayList<>();
            for (Role role : roles) {
                list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
            }
            return list;
        }
    }
    
    • 实现用户退出功能
      在spring-security配置文件中配置
    <security:logout invalidate-session="true" logut-url="/logout.do" logout-sucess-url="/login.jsp"/>
    
    • 实现用户查询功能
    • 导入页面user-list
    • 编写controller模块
    package org.hacker.ssm.controller;
    
    import org.hacker.ssm.domain.UserInfo;
    import org.hacker.ssm.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:55
     */
    public class UserController {
        @Autowired
        private IUserService userService;
    
        public ModelAndView findAll() {
            ModelAndView mv = new ModelAndView();
            List<UserInfo> userList = userService.findAll();
            mv.addObject("userlist", userList);
            mv.setViewName("user-list");
            return mv;
        }
    }
    
    • 添加代码到service接口及其实现类
    package org.hacker.ssm.service;
    
    import org.hacker.ssm.domain.UserInfo;
    import org.springframework.security.core.userdetails.UserDetailsService;
    
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:34
     */
    public interface IUserService extends UserDetailsService {
        List<UserInfo> findAll();
    }
    
    package org.hacker.ssm.service.impl;
    
    import org.hacker.ssm.dao.IUserDao;
    import org.hacker.ssm.domain.Role;
    import org.hacker.ssm.domain.UserInfo;
    import org.hacker.ssm.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:34
     */
    @Service("userService")
    @Transactional
    public class UserServiceImpl implements IUserService {
    
        @Autowired
        private IUserDao userDao;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UserInfo userInfo = null;
            try {
                userInfo = userDao.findByUsername(username);
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<Role> roles = userInfo.getRoles();
            //处理自己的用户对象封装成UserDetails
            User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(), userInfo.getStatus() == 0 ? false : true, true, true, true, getAuthority(userInfo.getRoles()));
            return user;
        }
    
        //作用就是返回一个List集合,集合中装入的是角色描述
        public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
    
            List<SimpleGrantedAuthority> list = new ArrayList<>();
            for (Role role : roles) {
                list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
            }
            return list;
        }
    
        @Override
        public List<UserInfo> findAll() {
            return userDao.findAll();
        }
    }
    
    • 添加代码到dao接口
    package org.hacker.ssm.dao;
    
            import org.apache.ibatis.annotations.Many;
            import org.apache.ibatis.annotations.Result;
            import org.apache.ibatis.annotations.Results;
            import org.apache.ibatis.annotations.Select;
            import org.hacker.ssm.domain.UserInfo;
            import org.springframework.stereotype.Service;
    
            import java.util.List;
    
    /**
     * @author HackerStar
     * @create 2020-04-24 11:41
     */
    public interface IUserDao {
        @Select("select * from users where username=#{username}")
        @Results({
                @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.itheima.ssm.dao.IRoleDao.findRoleByUserId"))
        })
        public UserInfo findByUsername(String username) throws Exception;
    
        @Select("select * from users")
        List<UserInfo> findAll();
    }
    
    • 项目运行结果
      img

    第三天完毕

  • 相关阅读:
    ORA-01940: cannot drop a user that is currently connected解决方法
    Git基本用法简介
    C 入门 第十节 存储区
    C 入门 第九节 结构体指针
    C 入门 第八节 指针
    C 入门 第七节 结构体
    C 入门 第六节 自定义函数
    C 入门 第五节 多维数组 字符串数组
    C 入门 第四节
    C 入门 第三节
  • 原文地址:https://www.cnblogs.com/artwalker/p/12858098.html
Copyright © 2011-2022 走看看