惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
C
Cisco Blogs
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
I
Intezer
GbyAI
GbyAI
Jina AI
Jina AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
Engineering at Meta
Engineering at Meta
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
小众软件
小众软件
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
Project Zero
Project Zero

博客园 - lodestar

一张图掌握数据存储 使用xtrabackup实现mysql定时热备份 开发篇1:使用原生api和Langchain调用大模型 预热篇2:从RNN到Transformmer 预热篇1:大模型训练显卡选型 macbook安装scala、hadoop、saprk环境 centos6.5 squid安装 一次linux服务器黑客入侵后处理 linux上搭建svn服务器 Windows系统下Oracle每天自动备份 android中listView的几点总结 使用template method模式简化android列表页面 andorid进度条使用 andorid定时器应用 - lodestar Android网络应用(第一部分) - lodestar 错误数据导致java.lang.IllegalArgumentException:Unsupported configuration attributes 使用ACEGI搭建权限系统:第三部分 acegi安全框架使用:第二部分 ajax实现用户名存在校验
使用ACEGI实现权限控制,第一部分
lodestar · 2011-12-27 · via 博客园 - lodestar

环境:
 struts2,acegi1.0.6,spring2.5.1,cas3

一、综述:环境搭建
1.spring和acegi版本
spring2.5.6和acegi1.0.6兼容性问题
会报
java.lang.NoClassDefFoundError: org/springframework/web/bind/RequestUtils
不过据说装合适的spring-webmvc这个jar也可以解决问题,没有测试

1.搭建acegi环境
web.xml配置,使用acegi的过滤器
配置web.xml
<context-param>中包含新配置文件
<!-- Acegi过滤器 -->
 <filter>
  <filter-name>acegiFilterChain</filter-name>
  <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
  <init-param>
   <param-name>targetClass</param-name>
   <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
  </init-param>
 </filter>

 <!-- Acegi过滤器URL映射 -->
 <filter-mapping>
  <filter-name>acegiFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

过滤器要放在struts前面,否则会报错。因为有些操作要acegi中的springmvc处理
javax.servlet.ServletException: Filter execution threw an exception
 org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
java.lang.NoClassDefFoundError: org/springframework/web/bind/RequestUtils
 
配置acegi配置文件,配置认证管理器、投票管理器、过滤器链、配置acegi的log4j日志

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">

 <!--
  ========================================================================
  认证管理器
  =========================================================================
 -->
 <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
  <property name="providers">
   <list>
    <ref bean="daoAuthenticationProvider" />
    <ref bean="rememberMeAuthenticationProvider" />
   </list>
  </property>
 </bean>
 
 <bean id="daoAuthenticationProvider"
  class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
  <property name="userDetailsService" ref="userDetailsService" />
  <property name="userCache" ref="userCache" />
 </bean>
   
 <!-- 基于DAO验证的Ehcache实现 -->
 <bean id="userCache"
  class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
  <property name="cache" ref="userCacheBackend" />
 </bean>
 <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  <property name="cacheManager" ref="cacheManager" />
  <property name="cacheName">
   <value>userCache</value>
  </property>
 </bean>
 <bean id="cacheManager"
  class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />

 <bean id="rememberMeAuthenticationProvider"
  class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
  <property name="key" value="remember_Me" />
 </bean>

 <!-- 使用数据库DAO查找用户和角色 -->
 <bean id="userDetailsService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
  <property name="dataSource" ref="dataSource" />
  <property name="usersByUsernameQuery">
   <value>
          <![CDATA[SELECT u.staffname USERNAME,u.staffpwd PASSWORD,u.STATUS enabled FROM sw_staff u where u.STATUS = 1 AND u.staffname = ?]]>
   </value>
  </property>
  <property name="authoritiesByUsernameQuery">
   <value>
          <![CDATA[SELECT t1.staffid USERNAME,t3.roledec authority FROM sw_staff t1, SW_UserPower t2,sw_Roles t3 WHERE t1.staffid= t2.staffid and t2.roleid= t3.roleid and t1.staffname = ?]]>
   </value>
  </property>
 </bean>

 <!--
  ========================================================================
  决策管理器
  =========================================================================
 -->
 <!-- 至少有一个投票者容许,就可以访问资源 -->
 <bean id="accessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
  <property name="decisionVoters">
   <list>
    <bean class="org.acegisecurity.vote.RoleVoter" />
   </list>
  </property>
  <!-- 所有投票者弃权,不容许访问 -->
  <property name="allowIfAllAbstainDecisions" value="false" />
 </bean>

 <!--
  ========================================================================
  过滤器链
  =========================================================================
 -->

 <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
  <property name="filterInvocationDefinitionSource">
   <value>
    CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
    PATTERN_TYPE_APACHE_ANT
    /**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,rememberMeFilter,exceptionFilter
   </value>
  </property>
 </bean>

 <!-- 从Session中获得用户信息并放入SecurityContextHolder -->
 <bean id="httpSessionContextIntegrationFilter"
  class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />

 <!-- 用户注销 -->
 <bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter">
  <!-- 注销后默认的跳转页面 -->
  <constructor-arg value="/helloWorld.jsp" />
  <constructor-arg>
   <list>
    <ref bean="rememberMeServices" />
    <bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler" />
   </list>
  </constructor-arg>
  <property name="filterProcessesUrl" value="/j_logout.do" />
 </bean>

 <!-- 表单方式验证用户身份 -->
 <bean id="authenticationProcessingFilter"
  class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
  <property name="authenticationManager" ref="authenticationManager" />
  <!-- 登录失败的页面,包含出错信息的登录页面 -->
  <property name="authenticationFailureUrl" value="/login.jsp?login_error=Login%20failed." />
  <!-- 登录后的跳转页面 -->
  <property name="defaultTargetUrl" value="/helloWorld.jsp" />
  <!-- 用户登录的URL -->
  <property name="filterProcessesUrl" value="/j_acegi_security_check" />
  <property name="rememberMeServices" ref="rememberMeServices" />
 </bean>

 <bean id="casProcessingFilter" class="org.acegisecurity.ui.cas.CasProcessingFilter">
  <property name="authenticationManager" ref="authenticationManager" />
  <!-- 登录后的跳转页面 -->
  <property name="defaultTargetUrl" value="/helloWorld.jsp" />
  <property name="filterProcessesUrl" value="/j_acegi_cas_security_check" />
  <!-- 认证失败后的跳转页面 -->
  <property name="authenticationFailureUrl" value="/login.jsp?login_error=Login%20failed." />
  <property name="rememberMeServices" ref="rememberMeServices"/>
 </bean>

 <!-- 记住用户登录信息 -->
 <!--<bean id="rememberMeFilter"
  class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
  <property name="authenticationManager" ref="authenticationManager" />
  <property name="rememberMeServices" ref="rememberMeServices" />
 </bean>
-->

 <!-- 处理登录异常或权限异常的Filter -->
 <bean id="exceptionFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
  <!-- 出现AuthenticationException时的登录入口,无认证异常 -->
  <!--
   表单登录时用 <property name="authenticationEntryPoint"> <bean
   class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
   将用户导向登录页面 <property name="loginFormUrl" value="/login.jsp" />
   是否强制使用https <property name="forceHttps" value="false" /> </bean>
   </property>
  -->
  <!--CAS认证登录时用-->
  <property name="authenticationEntryPoint">
   <bean class="org.acegisecurity.ui.cas.CasProcessingFilterEntryPoint">
    <property name="loginUrl"
     value="https://localhost:8443/cas-server-webapp-3.1.1/login" />
    <property name="serviceProperties" ref="serviceProperties" />
   </bean>
  </property>
  <!-- 出现AccessDeniedException时的Handler,无权限异常-->
  <property name="accessDeniedHandler">
   <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
    <property name="errorPage" value="/accessDenied.jsp" />
   </bean>
  </property>
 </bean>

 <bean id="serviceProperties" class="org.acegisecurity.ui.cas.ServiceProperties">
  <!-- CAS登录后的重定向页面-->
  <property name="service" value="http://localhost:8080/test" />
  <property name="sendRenew" value="false" />
 </bean>

 <bean id="securityInterceptor"
  class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
  <property name="authenticationManager" ref="authenticationManager" />
  <property name="accessDecisionManager" ref="accessDecisionManager" />
  <property name="objectDefinitionSource">
   <value>
    CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
    PATTERN_TYPE_APACHE_ANT
    /index.jsp=ROLE_LOGIN
    /*action=ROLE_LOGIN
   </value>
  </property>
 </bean>

说明:
1、过滤器链:filterChainProxy,有顺序要求
/**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,rememberMeFilter,exceptionFilter,securityInterceptor

2、rememberMe配置要点
rememberMeServices使用TokenBasedRememberMeServices,token64编码后放入cookie
logoutFilter使用rememberMeServices,离开后保存cookie

rememberMeFilter配置authenticationManager和rememberMeServices
rememberMeServices的parameter和html表单的Remember Me字段的name一致
rememberMeServices的key和authenticationManager中的rememberMeAuthenticationProvider的key要一致

3、使用DaoAuthenticationProvide实现基于数据库的用户认证授权,userDetailsService中可以用注入sql使用数据库实现,文件方式保存用户基本没啥用
 修改usersByUsernameQuery和authoritiesByUsernameQuery注入sql
 使用Ehache提高性能

4、配置日志
 <!-- 监听认证授权事件,输出日志 -->
    <bean id="authenticationLoggerListener"
  class="org.acegisecurity.event.authentication.LoggerListener" />

 <bean id="authorizationLoggerListener"
  class="org.acegisecurity.event.authorization.LoggerListener" />

对应的,如果是log4j.xml
log4j.xml
<logger name="log4j.logger.org.acegisecurity" additivity="true">
  <level value="DEBUG" />
  <appender-ref ref="LOG.DEBUG" />
</logger>
如果是log4j.properties
log4j.properties
 log4j.logger.org.acegisecurity=DEBUG  


5.表单认证
<bean id="authenticationProcessingFilter"
  class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
  <property name="authenticationManager" ref="authenticationManager" />
  <!-- 登录失败的页面,包含出错信息的登录页面 -->
  <property name="authenticationFailureUrl" value="/login.jsp?login_error=Login%20failed." />
  <!-- 登录后的跳转页面 -->
  <property name="defaultTargetUrl" value="/helloWorld.jsp" />
  <!-- 用户登录的URL -->
  <property name="filterProcessesUrl" value="/j_login.do" />
  <property name="rememberMeServices" ref="rememberMeServices" />
 </bean>
 表单中的username和password的name这里为j_username和j_password,acegi的springmvc实现中用
 filterProcessesUrl默认是j_acegi_security_check,可以修改,需要登录页面同步

6.用户注销
 logoutFilter中filterProcessesUrl默认是j_acegi_logout,只要将退出页面中url设置为j_acegi_logout就可以触发


7.授权
accessDecisionManager:事前评估。使用投票表决器。RoleVoter是基于角色的投票表决器。
这里securityInterceptor使用文件配置,也可以修改为数据库方式实现

8.使用aop对方法授权

第二部分:介绍dbms实现鉴权,包括资源过滤和方法过滤,表结构模型

第三部门:和CAS集成,包括CAS环境搭建,acegi集成CAS