安全管理 -Spring-Security
简单例子:
1:加入spring-Securtiy 的jar包
2:在web.xml中配置
<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>
3:修改applicationContext-security.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ss="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-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<!--最简单配置-->
<ss:http auto-config="true">
url拦截信息 /** 所有的
<ss:intercept-url pattern="/**" access="ROLE_ADMIN"/>
</ss:http>
<ss:authentication-provider>
<ss:user-service> 用户的认证 authorities 权限
<ss:user password="admin" name="admin" authorities="ROLE_ADMIN"/>
<!--在第5步完成后添加一个用户试验 -->
输入用户名user,密码user 会出现403 访问拒绝的错误,原因是
权限ROLE_USER 在ss:intercept-url 没有拦截任何url
如果修改上面如下:<ss:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER"/>
就没有问题
<ss:user password="user" name="user" authorities="ROLE_USER"/>
</ss:user-service>
</ss:authentication-provider>
</beans>
4:启动项目
5:http://localhost:8080/fpm/回车
会自动出现一个spring-security的一个登录界面(自动生产的登录界面)
输入admin 和admin 就可以进入,否则会提示错误信息
6:登录界面是系统给的。接下来是一个自定义的登录界面
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FPM -- 管理系统</title>
<%@ include file="/common/metas.jsp"%>
</head>
<body>
action,name 用spring-security的固定格式
<form name="loginForm" action="${path}/j_spring_security_check" method="POST">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="j_username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="j_password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
7:修改applicationContext-security.xml如下
在<ss:http auto-config="true">中增加如下代码
<ss:form-login
login-page="登录页面"
authentication-failure-url="登录失败页面"
default-target-url="登录成功的页面"
/>
8:启动项目,输入http://localhost:8080/fpm/ 发现进去不了
原因是:web.xml中的过滤器拦截了这个,然后跳转到form-login中的login-page中
但是login-page 又被拦截,所以限入了死循环了
在这句前面加上<ss:intercept-url pattern="/**" access="ROLE_ADMIN"/>
<ss:intercept-url pattern="登录页面" filters="none"/> 表示登录页面不被过滤
9:怎么用数据库去存储用户,密码(这都是spring-security的默认方式)
create table users( --用户表
username varchar2(50) not null,
password varchar2(50) not null,
enabled char(1) not null --当前用户是否可用 boolean 也可以
);
create table authorities ( --权限表
username varchar2(50) not null, --用户
authority varchar2(50) not null --权限
);
insert into users(username,password,enabled) values('admin','admin','1');
insert into users(username,password,enabled) values('user','user','1');
insert into authorities(username,authority) values('admin','ROLE_ADMIN');
insert into authorities(username,authority) values('admin','ROLE_USER');
insert into authorities(username,authority) values('user','ROLE_USER');
10:修改applicationContext-security.xml如下
先禁止<ss:authentication-provider> 然后添加如下
<ss:authentication-provider>
<ss:jdbc-user-service data-source-ref="spring中数据源名字"/>
</ss:authentication-provider>
11:运行项目 访问数据库中的用户 也可以。
12:修改applicationContext-security.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ss="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-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<ss:http auto-config="true">
<ss:intercept-url pattern="/common/**" filters="none"/>
<ss:intercept-url pattern="/css/**" filters="none"/>
<ss:intercept-url pattern="/images/**" filters="none"/>
<ss:intercept-url pattern="/js/**" filters="none"/>
IS_AUTHENTICATED_ANONYMOUSLY 表示匿名权限,所有匿名都来login.action中
<ss:intercept-url pattern="/login.action" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<ss:intercept-url pattern="/company/company.action" access="ROLE_ADMIN"/>
<ss:intercept-url pattern="/dept/dept.action" access="ROLE_USER"/>
IS_AUTHENTICATED_FULLY 默认权限,只要登录就可以访问(不管是admin还是其他权限)
<ss:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY"/>
<ss:form-login
login-page="/login.action"
authentication-failure-url="/login.action?error=true"
default-target-url="/"
/>
</ss:http>
<!--<ss:authentication-provider>
<ss:user-service>
<ss:user password="admin" name="admin" authorities="ROLE_ADMIN,ROLE_USER"/>
<ss:user password="user" name="user" authorities="ROLE_USER"/>
</ss:user-service>
</ss:authentication-provider>
-->
<ss:authentication-provider>
<ss:jdbc-user-service data-source-ref="dataSource"/>
</ss:authentication-provider>
</beans>
13:前面的都是默认的数据库
自定义数据库用户权限管理
create table test_users( --用户表
id varchar2(100) not null primary key,
username varchar2(200) not null,
password varchar2(200) not null,
enabled char(1) not null --当前用户是否可用 boolean 也可以
);
数据为
1 admin admin 1
2 user user 1
create table test_role(
id varchar2(100) not null primary key,
role_name varchar2(100) not null
)
数据为
1 ROLE_ADMIN
2 ROLE_USER
create table test_user_role(
user_id varchar2(100),
role_id varchar2(100)
)
数据为
1 1
1 2
2 2
修改applicaitonContext-security.xml
<ss:authentication-provider>
<ss:password-encoder hash="md5"/>
<ss:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from test_user where username = ?"
authorities-by-username-query="select u.username,r.role_name authority from test_user u
join test_user_role ur on ur.user_id = u.id
join test_role r on ur.role_id = r.id
where u.username = ?"/>
</ss:authentication-provider>
users-by-username-query:由用户名查询出用户
authorities-by-username-query:由用户名查询出权限
14:对数据加密(md5)
posted on 2010-07-03 10:55
gjy 阅读(864)
评论(0) 编辑 收藏 所属分类:
ssh心得笔记