纳金网
标题:
spring acegi 官方例子2
[打印本页]
作者:
Mr.cai
时间:
2011-7-29 17:53
标题:
spring acegi 官方例子2
<!-- Start authentication config -->
<bean id="authenticationManager"
class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref bean="daoAuthenticationProvider" />
</list>
</property>
</bean>
<bean id="daoAuthenticationProvider"
class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService">
<ref bean="userDetailsService" />
</property>
</bean>
<!-- Authentication using In-memory Dao -->
<bean id="userDetailsService"
class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
<property name="userMap">
<value>
jklaassen=4moreyears,ROLE_ADMIN
bouerj=ineedsleep,ROLE_USER
</value>
</property>
</bean>
<!-- Authentication using JDBC Dao -->
<!--
<bean id="userDetailsService"
class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
-->
<!-- End authentication config -->
<!-- Start authorization config -->
<bean id="accessDecisionManager"
class="org.acegisecurity.vote.UnanimousBased">
<property name="decisionVoters">
<list>
<ref bean="roleVoter" />
</list>
</property>
</bean>
<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
<property name="rolePrefix">
<value>ROLE_</value>
</property>
</bean>
<!-- End authorization config -->
<!-- ****** END ACEGI Security Configuration *******-->
以上就是所有的源代码配置在spring 的配置文件中。详细的说明在官方的文档上。
在上面的配置文件的方式是以in-memory 的方法,也就是在配置文件中指定登录的用户名及密码。在实际的应用中,应用到数据库或其它技术。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <bean id="userDetailsService"
class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1</value>
</property>
</bean>
以上两个bean的代码就是把信息存储到数据库中。
sql 语句如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->CREATE TABLE `users` (
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`enabled` varchar(50) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users` VALUES ('dianne','emu','true');
INSERT INTO `users` VALUES ('marissa','koala','true');
INSERT INTO `users` VALUES ('peter','opal','true');
INSERT INTO `users` VALUES ('scott','wombat','true');
CREATE TABLE `authorities` (
`username` varchar(50) NOT NULL,
`authority` varchar(50) NOT NULL,
UNIQUE KEY `ix_auth_username` (`username`,`authority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `authorities` VALUES ('dianne','ROLE_ADMIN');
INSERT INTO `authorities` VALUES ('marissa','ROLE_ADMIN');
INSERT INTO `authorities` VALUES ('marissa','ROLE_USER');
INSERT INTO `authorities` VALUES ('peter','ROLE_USER');
INSERT INTO `authorities` VALUES ('scott','ROLE_ADMIN');
ALTER TABLE `authorities`
ADD FOREIGN KEY (`username`) REFERENCES `users` (`username`);
所有的配置就是这些:
理解一下原理:
1。acegi的添加,可以在程序写完之后再添加,配置灵活但并不简单。
2.四个步骤:
安全是实施这四项检查:
1 限制出入检查(是以资源担保? ) ;
2 现有的认证检查(有用户被认证? ) ;
3 如果没有有效的登录用户:认证要求退房(都是正确的用户名和密码提供? ) ;
4 授权入住(不含用户拥有所需的角色? ) ;
3.对于授权的处理,未授权的用户无法进行访问。应该设置 403.jsp未授权页面。
作者:
彬彬
时间:
2011-8-9 15:04
作者:
晃晃
时间:
2012-2-12 23:25
真是不错啊
作者:
C.R.CAN
时间:
2012-3-8 23:26
既来之,则看之!
作者:
菜刀吻电线
时间:
2012-3-9 23:21
爱咋咋地!
作者:
tc
时间:
2012-5-6 23:20
精典,学习了!
作者:
tc
时间:
2012-5-23 23:21
很有心,部分已收录自用,谢谢
作者:
奇
时间:
2012-6-20 23:25
“再次路过……”我造一个-----特别路过
作者:
奇
时间:
2012-7-3 23:21
发了那么多,我都不知道该用哪个给你回帖了,呵呵
作者:
菜刀吻电线
时间:
2012-8-28 00:21
好`我顶``顶顶
作者:
C.R.CAN
时间:
2012-9-4 23:19
很经典,很实用,学习了!
作者:
晃晃
时间:
2012-10-29 23:36
佩服,好多阿 ,哈哈
作者:
晃晃
时间:
2013-3-20 23:19
呵呵,真得不错哦!!
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5