spring security不同用户权限的设置

项目右键-MyEclipse-facet-install spring facet-

finish,spring security的jar包就加入到项目中了。(spring+hibernate环境也已经配置好了)

配置spring security:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>springSecurity2</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*.xml</param-value>
  </context-param>
  
        <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>  
  
</web-app>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

	<!-- 配置数据源 -->
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="url" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClassName" value="${jdbc.driverClass}"></property>

	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/template/security/Role.hbm.xml</value>
				<value>com/template/security/Authority.hbm.xml</value>
				<value>com/template/security/User.hbm.xml</value></list>
		</property></bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" /></beans>

security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	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-3.1.xsd">

	<import resource="applicationContext.xml"/>

	<security:http use-expressions="true">
		<security:intercept-url pattern="/**" access="hasRole(‘ROLE_ADMIN‘)" requires-channel="http"/>
		<security:form-login/>
		<security:remember-me/>
	</security:http>

	<security:authentication-manager>
		<security:authentication-provider>
			<security:jdbc-user-service data-source-ref="dataSource"
			users-by-username-query="select username,password,enabled as status from user where username=?"
			authorities-by-username-query="select u.username,r.name as authority from user u join authority a on a.userid=u.id join role r on r.id=a.roleid where u.username=?"/>
		</security:authentication-provider>
	</security:authentication-manager>

</beans>

在系统中定义用户,角色,权限这三种实体,一个用户可以拥有多个角色,一个角色可以被多个用户拥有,所以用户与角色之间是多对多的关系,为了易于理解,这里加入第三种实体权限,作为用户和角色的中间关联实体,把用户与角色间的多对多关系拆为两个一对多的关联关系。这样一个用户就对应着多个权限,一个权限对应着一个用户,而一个角色对应着多个权限,一个权限对应着一个角色。

User.java:

package com.template.security;

import java.util.List;

public class User {

	private Integer id;  
    private String username;  
    private String password;  
    private Boolean enabled;  
  
    private List<Authority> authorities;  
  
    public User() {  
    }

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public List<Authority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}  
    
    
}

User.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.template.security.User" table="user" catalog="spring_security">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="20" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="20" not-null="true" />
        </property>
        <property name="enabled" type="java.lang.Short">
            <column name="enabled" not-null="true" />
        </property>
        
        <bag name="authorities">
        	<key column="userid"/>
        	<one-to-many class="com.template.security.Authority"/>
        </bag>
    </class>
</hibernate-mapping>

Role.java:

package com.template.security;

public class Role {

	private Integer id;  
    private String name;  
  
    public Role() {  
    }

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}  
    
    
}

Role.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.template.security.Role" table="role" catalog="spring_security">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Authority.java:

package com.template.security;

public class Authority {

	private Integer id;  
    private User user;  
    private Role role;  
  
    public Authority() {  
    }

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}  
    
    
}

Authority.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.template.security.Authority" table="authority" catalog="spring_security">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="user" column="userid" class="com.template.security.User"></many-to-one>
        <many-to-one name="role" column="roleid" class="com.template.security.Role"></many-to-one>
    </class>
</hibernate-mapping>

db.property:

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_security

#...
时间: 2024-08-03 14:44:39

spring security不同用户权限的设置的相关文章

spring security 控制用户信息用户加密 缓存用户信息

1. MD5加密 任何一个正式的企业应用中,都不会在数据库中使用明文来保存密码的,我们在之前的章节中都是为了方便起见没有对数据库中的用户密码进行加密,这在实际应用中是极为幼稚的做法.可以想象一下,只要有人进入数据库就可以看到所有人的密码,这是一件多么恐怖的事情,为此我们至少要对密码进行加密,这样即使数据库被攻破,也可以保证用户密码的安全. 最常用的方法是使用MD5算法对密码进行摘要加密,这是一种单项加密手段,无法通过加密后的结果反推回原来的密码明文. 首先我们要把数据库中原来保存的密码使用MD5

Spring Security 动态url权限控制(三)

一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-plus 2.2.0 mysql 数据库 maven项目 Spring Security入门学习可参考之前文章: SpringBoot集成Spring Security入门体验(一)https://blog.csdn.net/qq_38225558/article/details/101754743

页面获取Spring Security登录用户

页面获取Spring Security登录用户 1.在session中取得spring security的登录用户名如下:${session.SPRING_SECURITY_CONTEXT.authentication.principal.username} spring security 把SPRING_SECURITY_CONTEXT 放入了session 没有直接把username 放进去.下面一段代码主要描述的是session中的存的变量, 存跳转时候的URLsession {SPRIN

基于Spring LDAP和Spring Security的用户认证和权限控制Web实现

利用LDAP服务,使用Spring LDAP,Spring Security实现Web项目用户认证和简单的权限控制.实现多系统账号统一. 1.基于EHR的LDAP用户信息 LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.LDAP目录以树状的层次结构来存储数据.如果你对自顶向下的DNS树或UNIX文件的目录树比较熟悉,也就很容易掌LDAP目录树这个概念了.就象DNS的主机名那样,LDAP目录记录的标识名(Dis

Spring Security自定义用户认证

具体代码地址 https://gitee.com/chuzhuyong/HandleSafer 自定义用户认证 通过自定义WebSecurityConfigurerAdapter类型的Bean组件,并重写configure(Authentication ManagerBuilder auth)方法, 可以实现自定义用户认证. 一.内存身份认证   In-Memory Authentication(内存身份认证)是最简单的身份认证方式,主要用于Security安全认证体验和测试 1.自定义WebS

铁威马NAS用户权限的设置

通过灵活的用户管理,您可以为家庭或者企业的每个成员创建用户账户.您可以管理每个用户的权限,如共享文件夹的访问权限或者存储的配额.1.登录铁威马TOS系统:2.控制面板--用户:3.选择要设置的用户,点击编辑:4.点击用户权限,可为用户设置相对应的共享文件夹的访问权限: 原文地址:https://blog.51cto.com/14382576/2407442

02 spring security 自定义用户认证流程

1. 自定义登录页面 (1)首先在static目录下面创建login.html       注意: springboot项目默认可以访问resources/resources, resources/staic, resources/public目录下面的静态文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录页

MySql DCL数据控制语言(对用户权限的设置)

数据控制语言(DCL:Data Control Language)是用来设置或者更改数据库用户或角色权限的语句,这些语句包括 GRANT.DENY.REVOKE 等语句 1.限制root用户指定ip登录 查看root用户可以在哪台机器登录 select user,host from mysql.user where user='root'; 修改mysql库里边的user表 update mysql.user set host='localhost' where user='root'; 刷新权

第二章 &nbsp; linux用户权限的设置与更改

一.cat /etc/passwd 查看用户 su 切换到root用户 su - student 切换到普通用户 二.chown,改变文件属主 su 与su- 区别,chown  chmod chown改变文件属主与属组 chmod 改变文件权限