整合Acegi使用HTTPS安全通道(SSL)

首先在你使用application server上激活SSL,让项目能使用HTTPS。

下面以我使用的tomcat为例(其实网上也很多了,再啰嗦一遍方法):

1、生成及导入证书。

生成keystore

keytool -genkey -alias projectName -keypass changeit -storepass changeit -keyalg RSA -validity 3600 -dname "CN=127.0.0.1, OU=HOME, O=HOME" -keystore my.keystore

导出证书

keytool -export -alias projectName -file foo.cer -keystore my.keystore

导入证书到cacerts(注意该cacerts确保是你application server所使用的JDK的)

keytool -import -alias projectName -keystore cacerts -file foo.cer -trustcacerts

2、配置tomcat的 server.xml文件。
大概找到下面注释的一行。


<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->


把这行下面注释的代码去掉,如下:


<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
enableLookups="false" disableUploadTimeout="true" keystorePass="changeit"
maxThreads="150" scheme="https" secure="true" minSpareThreads="25"
clientAuth="false" sslProtocol="TLS" />



留意changeit字段,需要跟之前生成证书时输入的密码一致。

3、重启tomcat。

测试 https://localhost:8443 是否访问正常。


[size=medium][b]让项目同时使用http和https[/b][/size]

完成以上操作后,项目已经可以使用SSL来访问了,但也众所周知SSL之所以安全性比较高因为使用安全通道加密的缘故。所以原来http的一个请求和响应在https里面都变成几个,资源消耗大很多。所以我们有时候仅仅是为了一些资源使用https足以,并不是全部。

如果你是使用Acegi(spring security)的话,集成将非常简单。

具体配置如下:

打开acegi的配置文件。

1、找到exceptionFilter一段,具体代码如下:


<!-- 处理登录异常或权限异常的Filter -->
<bean id="exceptionFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
<!-- 出现AuthenticationException时的登录入口 -->
<property name="authenticationEntryPoint">
<bean
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl"
value="${exceptionFilter.AuthException.loginFormUrl}" />
<!-- in order to enable the https -->
<property name="forceHttps" value="true" />
</bean>
</property>
<!-- 出现AccessDeniedException时的Handler -->
<property name="accessDeniedHandler">
<bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
<!-- 指定错误页面 -->
</bean>
</property>
</bean>


留意把forceHttps的值设置为true。

2、配置channelDecisionManager

添加如下代码:

<bean id="channelDecisionManager"
class="org.acegisecurity.securechannel.ChannelDecisionManagerImpl">
<property name="channelProcessors">
<list>
<ref bean="secureChannelProcessor" />
<ref bean="insecureChannelProcessor" />
</list>
</property>
</bean>

<bean id="secureChannelProcessor" class="org.acegisecurity.securechannel.SecureChannelProcessor" />
<bean id="insecureChannelProcessor"
class="org.acegisecurity.securechannel.InsecureChannelProcessor" />


3、配置ChannelProcessingFilter,让我们仅对需要的SSL的资源使用https。


<bean id="channelProcessingFilter" class="org.acegisecurity.securechannel.ChannelProcessingFilter">
<property name="channelDecisionManager" ref="channelDecisionManager" />
<property name="filterInvocationDefinitionSource">
<value>
PATTERN_TYPE_APACHE_ANT
/admin/**=REQUIRES_SECURE_CHANNEL
/**=REQUIRES_INSECURE_CHANNEL
</value>
</property>
</bean>

(例子中为访问/admin/**资源的的时候必须使用https,若用http访问的时候也会直接转到https通道。其他资源可使用https也可以使用http。)

顾名思义:
REQUIRES_SECURE_CHANNEL 为使用https
REQUIRES_INSECURE_CHANNEL 为使用http

/** 注意请务必放在最后

另:请注意项目中的url地址,因为如果在https链接当中访问http资源的话,某些时候会出现问题,例如获取session和request之类的时候,实质上是从https进行了Redirect,而且还是那比较难查错。


使用acegi的话,应该配置过FilterSecurityInterceptor,相信格式也很熟悉了吧。

4、勿忘在FilterChainProxy添加新的filter
	<bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=sessionIntegrationFilter,logoutFilter,authenticationFilter,rememberMeFilter,channelProcessingFilter,exceptionFilter,securityInterceptor
</value>
</property>
</bean>


注意添加上channelProcessingFilter即可。

至此,已经可以享受Acegi在安全性上给我们带来的便利以及乐趣。

但实话说,有些情况下使用Acegi却带来很多的麻烦,或者是必须深入了解,不然扩展性和易用性还是有限。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值