Spring Security

本文详细介绍了使用 Spring Security 2.0.5 版本进行 Web 应用安全配置的方法,包括登录验证流程、权限分配及数据库连接配置等关键步骤。

我的开发环境是tomcat6 jdk6 mysql5 spring2.5和spring scurity2.0.5 开发的文件如下:
<?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-2.5.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<http auto-config='true'>
<intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/>
<intercept-url pattern="/**" access="ROLE_USER"/>
<form-login login-page="/login.jsp"
  authentication-failure-url="/login.jsp?error=true"
  default-target-url="/" />

</http>
 <authentication-provider>
  <jdbc-user-service data-source-ref="dataSource"
  users-by-username-query="select username,password,status as enabled
  from user
  where username=?"
  authorities-by-username-query="select u.username,r.name as authority
  from user u
  join user_role ur
  on u.id=ur.user_id
  join role r
  on r.id=ur.role_id
  where u.username=?"/>
  </authentication-provider>

  <beans:bean id="filterSecurityInterceptor"
  class="org.springframework.security.intercept.web.FilterSecurityInterceptor" autowire="byType">
  <custom-filter before="FILTER_SECURITY_INTERCEPTOR" />
  <beans:property name="objectDefinitionSource" ref="filterInvocationDefinitionSource" />
  </beans:bean>

  <beans:bean id="filterInvocationDefinitionSource"
  class="JdbcFilterInvocationDefinitionSourceFactoryBean">
  <beans:property name="dataSource" ref="dataSource"/>
  <beans:property name="resourceQuery" value="
  select re.res_string,r.name
  from role r
  join resc_role rr
  on r.id=rr.role_id
  join resc re
  on re.id=rr.resc_id
  order by re.priority
  "/>
  </beans:bean>
   
   
  <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  >
  <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <beans:property name="url" value="jdbc:mysql://localhost:3306/tests"/>
  <beans:property name="username" value="root"/>
  <beans:property name="password" value="root"/>
  </beans:bean>
 

</beans:beans>
这是我的配置文件下面是JdbcFilterInvocationDefinitionSourceFactoryBean.java
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.FactoryBean;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;

import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ConfigAttributeEditor;
import org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.FilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.RequestKey;
import org.springframework.security.util.AntUrlPathMatcher;
import org.springframework.security.util.UrlMatcher;


public class JdbcFilterInvocationDefinitionSourceFactoryBean
  extends JdbcDaoSupport implements FactoryBean {
  private String resourceQuery;

  public boolean isSingleton() {
  return true;
  }

  public Class getObjectType() {
  return FilterInvocationDefinitionSource.class;
  }

  public Object getObject() {
  return new DefaultFilterInvocationDefinitionSource(this
  .getUrlMatcher(), this.buildRequestMap());
  }

  protected Map<String, String> findResources() {
  ResourceMapping resourceMapping = new ResourceMapping(getDataSource(),
  resourceQuery);

  Map<String, String> resourceMap = new LinkedHashMap<String, String>();

  for (Resource resource : (List<Resource>) resourceMapping.execute()) {
  String url = resource.getUrl();
  String role = resource.getRole();

  if (resourceMap.containsKey(url)) {
  String value = resourceMap.get(url);
  resourceMap.put(url, value + "," + role);
  } else {
  resourceMap.put(url, role);
  }
  }

  return resourceMap;
  }

  protected LinkedHashMap<RequestKey, ConfigAttributeDefinition> buildRequestMap() {
  LinkedHashMap<RequestKey, ConfigAttributeDefinition> requestMap = null;
  requestMap = new LinkedHashMap<RequestKey, ConfigAttributeDefinition>();

  ConfigAttributeEditor editor = new ConfigAttributeEditor();

  Map<String, String> resourceMap = this.findResources();

  for (Map.Entry<String, String> entry : resourceMap.entrySet()) {
  RequestKey key = new RequestKey(entry.getKey(), null);
  editor.setAsText(entry.getValue());
  requestMap.put(key,
  (ConfigAttributeDefinition) editor.getValue());
  }

  return requestMap;
  }

  protected UrlMatcher getUrlMatcher() {
  return new AntUrlPathMatcher();
  }

  public void setResourceQuery(String resourceQuery) {
  this.resourceQuery = resourceQuery;
  }

  private class Resource {
  private String url;
  private String role;

  public Resource(String url, String role) {
  this.url = url;
  this.role = role;
  }

  public String getUrl() {
  return url;
  }

  public String getRole() {
  return role;
  }
  }

  private class ResourceMapping extends MappingSqlQuery {
  protected ResourceMapping(DataSource dataSource,
  String resourceQuery) {
  super(dataSource, resourceQuery);
  compile();
  }

  protected Object mapRow(ResultSet rs, int rownum)
  throws SQLException {
  String url = rs.getString(1);
  String role = rs.getString(2);
  Resource resource = new Resource(url, role);

  return resource;
  }
  }
}
打开login。jsp的速度超慢,半个小时都打不开我刷新了下在我的tomcat管理页面里出现如下
Path Display Name Running Sessions Commands  
test1 true 2  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值