搞了一天的Spring Security,从早折腾到黑。在这把思路整理一下,为需要的朋友做个参考。
主要参考了http://blog.csdn.net/csuliky/archive/2009/06/17/4277413.aspx的文章及Spring官网的例子。
首先配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!--让容器找到spring的配置文件-->
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
然后配置security.xml(名称随便起,想叫什么都行),我把它放到source根目录,跟applicationContext.xml放到一起
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<global-method-security pre-post-annotations="enabled">
<!-- AspectJ pointcut expression that locates our "post" method and applies security that way
<protect-pointcut expression="execution(* bigbank.*Service.post*(..))" access="ROLE_TELLER"/>
-->
</global-method-security>
<http use-expressions="true">
<intercept-url pattern="/secure/extreme/**" access="hasRole('ROLE_SUPERVISOR')"/>
<intercept-url pattern="/secure/**" access="isAuthenticated()" />
<!-- Disable web URI authorization, as we're using <global-method-security> and have @Secured the services layer instead
<intercept-url pattern="/listAccounts.html" access="isRememberMe()" />
<intercept-url pattern="/post.html" access="hasRole('ROLE_TELLER')" />
-->
<intercept-url pattern="/**" access="permitAll" />
<form-login />
<logout />
<remember-me />
<!--
Uncomment to enable X509 client authentication support
<x509 />
-->
<!-- Uncomment to limit the number of sessions a user can have -->
<session-management invalid-session-url="/timeout.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http>
<!--
Usernames/Passwords are
rod/koala
dianne/emu
scott/wombat
peter/opal
-->
<authentication-manager>
<!-- <authentication-provider>
<password-encoder hash="md5"/>
<user-service>
<user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
<user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
<user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
<user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER" />
</user-service>
</authentication-provider>-->
<authentication-provider>
<password-encoder hash="md5"/>
<jdbc-user-service data-source-ref="myDataSource" />
</authentication-provider>
</authentication-manager>
</beans:beans>
主要的配置文件就是这样了,在页面上也要进行一些设置,就不再一一例举了,把demo传上来,需要的下载开代码就明白了。
这是一个Spring Security的数据库认证实例,要注意以下几点:
(1)请自行加入Spring必须的包,Spring security的包和MySQL的驱动包,当然你也可以换成其他的数据库,但是你要相应的修改spring-common.xml中的dataSource部分
(2)数据库中的两个表users,authorites必须完全按照脚本所示来定义,也就是说表的名字不能修改.
(3)users表必须包含username,password,enabled字段,这三个字段是绝对不能少的,也不能修改类型.另外enabled一定要为1才能登录
(4)authorities表必须包含username字段,这个字段引用users的username作为外键,authority字段就是角色的名字,角色名字必须满足ROLE_XXX的格式(例如:ROLE_ADMIN,ROLE_USER,ROLE_MAMAGER)
(5)如果一个用户有多个角色,不要将多个角色放在一起用逗号隔开.而是每个角色定义一条记录(例如:abu有ROLE_ADMIN,ROLE_USER两个角色,那么应该定义两条记录: 一条为abu, ROLE_USER,另一条为abu, ROLE_ADMIN.而不是只有一条:abu, ROLE_ADMIN,ROLE_USER)
(6)你可以给authorities表添加一个id字段作为主键.
/-- 注意这里的脚本是MYSQL的,因此在你演示这个实例的时候,要加入MySQL的驱动包 --/
create table users
(
username varchar(50) primary key,
password varchar(50),
enabled tinyint(1)
);
create table authorities
(
id int auto_increment primary key,
username varchar(50),
authority varchar(50),
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
分享到:
相关推荐
基于springMVC+mybatis+mysql实现。基于SpringSecurity权限控制的简单工程DEMO。maven构建.
一个学习Spring Security权限控制的demo,使用的IDE是idea。
非常详细的Spring Security 权限控制中文API
基于SpringSecurity+springMVC+mybatis+mysql实现。基于SpringSecurity权限控制的简单工程DEMO。maven构建,用户信息存储在mysq数据库中
SpringSecurity+OAuth2+JWT分布式权限控制
springboot springsecurity动态权限控制,实现数据库动态管理菜单权限
详情地主:https://blog.csdn.net/qq_38425662/article/details/82703128
项目是由maven管理,springboot架构,springsecurity权限管理组成的万能的系统管理后台模板。希望对大家有帮助,谢谢!
本篇文章主要介绍了java中自定义Spring Security权限控制管理示例(实战篇) ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
SpringSecurity,这是一种基于Spring AOP和Servlet过滤器的安全框架。...本实例简单配置了springsecurity权限控制,提供admin,user两个用户,在页面登录时以不同角色可访问不同页面,初学者可加以参考此例 谢谢!
主要介绍了SpringSecurity权限控制实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.Spring Security权限控制 2.结合BCrypt加密 3.thymeleaf模板 4.小型mode,简单明了
struts2 + spring3 + hibernate3 + spring security3 + mysql + tomcat sys_users;sys_roles;sys_authorities;sys_resources;sys_users_roles;sys_roles_authorities;sys_authorities_resources; PS:此项目运行不...
看B站视频项目总结的系列文档,仅供个人学习使用
SpringBoot整合权限控制SpringSecurity
SpringSecurity权限管理
Spring Security权限管理开发手册
本项目基于Spring,整合Spring的security模块,实现用户管理和权限控制,是一套较为通用的权限控制功能,主要内容如下: 1.登录,包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始密码1234; 3.拦截器...
主要介绍了Spring Security如何使用URL地址进行权限控制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下