Spring-security 1
?有没有发现一个问题,我们之前做的所有练习,都没的权限管理这个模块。我们的WEB应用中的同一个帐户可以在多台机器上同时登陆,每一个用户可以操作所有功能模块。这样在以后的应用开发中是结对不可行的!
??
???????? 首先让我们来回想一下,以前老方在讲JavaWEB基础时向大家介绍使用Filter进行权限管理,这是一个十分好的方法。当然他也有说,以后大家学习Spring时,只要简单的配置几下就可以进行权限管理了,十分方便。(注意:过滤器)
·???????? 防止一个帐户同时在多个机器上登陆
·???????? 帐户只能根据自己的权限操作相应的功能模块
·???????? 帐户只能在页面上看到具有对应权限的数据内容
<!-- 搭建Spring -->
<context-param>
?????? <param-name>contextConfigLocation</param-name>
?????? <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
?????? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
?
<!-- 添加Spring-Security过滤器 -->
<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>?
<!-- 配置SpringSecurity的http安全服务 -->
<sec:http auto-config="true">
?????? <!-- 只有ROLE_ADMIN或ROLE_USER权限用户才能访问index.jsp -->
?????? <sec:intercept-url pattern="/index.jsp" access="ROLE_ADMIN,ROLE_USER"/>
?????? <!-- 只有ROLE_ADMIN权限用户才能访问admin.jsp -->
???????? <sec:intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/>
</sec:http>
?
<!-- 配置SpringSecutiry的权限信息 -->
<sec:authentication-provider>
?????? <sec:user-service>
????????????? <!-- 帐户信息 -->
????????????? <sec:user password="admin" name="admin" authorities="ROLE_ADMIN"/>
????????????? <sec:user password="user" name="user" authorities="ROLE_USER"/>
?????? </sec:user-service>
</sec:authentication-provider>
<body>
?????? <h1>*****INDEX*****</h1>
????????????? <security:authorize ifAllGranted="ROLE_ADMIN">
???????????????????? <br>
???????????????????? <a href="admin.jsp">ADMIN</a>
????????????? </security:authorize>
?????? <br>
?????? <a href="logout">LOGOUT</a>
</body>
uri="http://www.springframework.org/security/tags"%>。
<body>
?????? <h1>*****ADMIN*****</h1>
?????? <a href="index.jsp">RETURN</a>
</body>
??
???????? 我们没有为工程添加这们的页面啊!先不管这些,使用我们配置的两个帐户登陆看看功能如何。
<!-- 配置登出信息 -->
<logout logout-url="/logout" logout-success-url="/bye.jsp"
?????? invalidate-session="true" />
<!-- 配置登陆信息 -->
<form-login login-page="/login.jsp" login-processing-url="/login" />
<!-- 配置登陆信息 -->
<form-login login-page="/login.jsp"
?????? login-processing-url="/login"???????????????????
???????? default-target-url="/index.jsp"
?????? authentication-failure-url="/loginfail.jsp"
?????? always-use-default-target="true"/>
login-page:登陆页面,这里的login.jsp页面中的表单请求和字段名称必须是spring-security指定的,可以查看spring-security默认为我们提供的页面代码。
login-processing-url:登陆请求处理URL。
default-target-url:登陆成功后,转到的页面。
authentication-failure-url:登陆失败后,转到的页面。
always-use-default-target:一直使用”default-target-url“,这是什么意思?假如我们现在有两个页面可以跳到登陆页面,默认情况下spring-security在登陆成功后会跳转到——跳转登陆页面的页面。如果这个值真,那么登陆成功后会一直跳转到”default-target-url“指定的页面。有些情况下,用户直接进入登陆页面进行登陆,此时登陆成功后会跳转到”default-target-url“指定的页面,如果没有指定这个页面则跳转到index.jsp页面,如果没有index.jsp页面将出错。
?
问题:
?????? 将这么重要的信息放在spring的配置文件中是相当不安全的,Spring-security为们提供了安全的方法,将数据放到数据库中。
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
?????? <property name="user" value="${user}" />
?????? <property name="password" value="${password}" />
?????? <property name="driverClass" value="${driverClass}" />
?????? <property name="jdbcUrl" value="${jdbcUrl}" />
?
?????? <property name="minPoolSize" value="5" />
?????? <property name="initialPoolSize" value="3" />
?????? <property name="maxPoolSize" value="10" />
?????? <property name="acquireIncrement" value="2" />
</bean>
<bean id="jdbcTemplate"
?????? class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
?????? <constructor-arg ref="dataSource"/>
</bean>
??
???????? 2).role(角色表)
??
???????? 3).user_role(中间表)
??
package cn.itcast.cc.spring.security;
?
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.User;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
?
@Component("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
?
?????? @Resource
?????? private SimpleJdbcTemplate jdbcTemplate = null;
?
?????? @Override
?????? public UserDetails loadUserByUsername(String username)
???????????????????? throws UsernameNotFoundException, DataAccessException {
????????????? // 根据用户名获取帐户和权限信息
????????????? String sql = "SELECT username,password,status,name rname"
??????????????????????????? + " FROM user u,role r,user_role ur"
??????????????????????????? + " WHERE u.id=ur.user_id AND r.id=ur.role_id AND username=?";
????????????? // 如果一个用户具有多个权限,连接查询会返回一个List
????????????? List list = this.jdbcTemplate.queryForList(sql,
??????????????????????????? new Object[] { username });
?
????????????? // 取出帐户和权限信息填充到User中返回
????????????? if (list == null || list.size() <= 0)
???????????????????? // spring-security定义的异常
???????????????????? throw new UsernameNotFoundException("用户不存在!");
????????????? // 如果用户存在
????????????? Map<String, Object> map = (Map<String, Object>) list.get(0);
????????????? // 密码
????????????? String password = (String) map.get("password");
????????????? // 帐户是否可用
????????????? boolean enabled = ((Integer) map.get("status") == 1);
????????????? // 帐户所具有的权限
????????????? GrantedAuthority[] gas = new GrantedAuthority[list.size()];
????????????? for (int i = 0; i < gas.length; i++) {
???????????????????? Map<String, Object> temp = (Map<String, Object>) list.get(i);
???????????????????? gas[i] = new GrantedAuthorityImpl((String) temp.get("rname"));
????????????? }
????????????? // spring-security提供的类
????????????? User user = new User(username, password, enabled, true, true, true, gas);
????????????? return user;
?????? }
}
<authentication-provider user-service-ref="userDetailsService" />
?
???????? 3).运行,跑一个!
?
???????? Spring-security的实现原理是什么?查看它的源文件,东西似乎还不少。我们简单的想一下,这些功能在Filter中完全可以实现。我们可以在Filter中读取数据库的记录,根据帐户的权限决定是否允许用户继续访问。我们也可以在JSP页面中添加代码,判断用户的权限以显示或隐藏相应的功能。
???????? 如果我们手动去实现这样的功能,那还是有些麻烦的。
?
???????? 明天继续Spring-sceurity!
http://www.blogjava.net/changcheng/archive/2010/02/03/311856.html