第十四章 SSL——《跟我学Shiro》

 

目录贴: 跟我学Shiro目录贴

 

对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问。

 

首先生成数字证书,生成证书到D:\localhost.keystore

使用JDKkeytool命令,生成证书(包含证书/公钥/私钥)到D:\localhost.keystore

keytool -genkey -keystore "D:\localhost.keystore" -alias localhost -keyalg RSA

输入密钥库口令:

再次输入新口令:

您的名字与姓氏是什么?

  [Unknown]:  localhost

您的组织单位名称是什么?

  [Unknown]:  sishuok.com

您的组织名称是什么?

  [Unknown]:  sishuok.com

您所在的城市或区域名称是什么?

  [Unknown]:  beijing

您所在的省//自治区名称是什么?

  [Unknown]:  beijing

该单位的双字母国家/地区代码是什么?

  [Unknown]:  cn

CN=localhost, OU=sishuok.com, O=sishuok.com, L=beijing, ST=beijing, C=cn是否正确

?

  []:  y

 

输入 <localhost> 的密钥口令

        (如果和密钥库口令相同按回车):

再次输入新口令:

 

通过如上步骤,生成证书到D:\ localhost.keystore

 

然后设置tomcat下的server.xml

此处使用了apache-tomcat-7.0.40版本,打开conf/server.xml,找到:

 

<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
       maxThreads="150" scheme="https" secure="true"
       clientAuth="false" sslProtocol="TLS" />
--> 

替换为  

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
       maxThreads="150" scheme="https" secure="true"
       clientAuth="false" sslProtocol="TLS" 
       keystoreFile="D:\localhost.keystore" keystorePass="123456"/> 

keystorePass就是生成keystore时设置的密码。

 

添加SSL到配置文件(spring-shiro-web.xml

此处使用了和十三章一样的代码:

<bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter">
    <property name="port" value="8443"/>
</bean>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    ……
    <property name="filters">
        <util:map>
            <entry key="authc" value-ref="formAuthenticationFilter"/>
            <entry key="ssl" value-ref="sslFilter"/>
        </util:map>
    </property>
    <property name="filterChainDefinitions">
        <value>
            /login.jsp = ssl,authc
            /logout = logout
            /authenticated.jsp = authc
            /** = user
        </value>
    </property>
</bean> 

SslFilter默认端口是443,此处使用了8443;“/login.jsp = ssl,authc”表示访问登录页面时需要走SSL。

 

测试

最后把shiro-example-chapter14打成war包(mvn:package),放到tomcat下的webapps中,启动服务器测试,如访问localhost:9080/chapter14/,会自动跳转到https://localhost:8443/chapter14/login.jsp

 

如果使用Maven Jetty插件,可以直接如下插件配置: 

<plugin>
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>jetty-maven-plugin</artifactId>
   <version>8.1.8.v20121106</version>
   <configuration>
     <webAppConfig>
       <contextPath>/${project.build.finalName}</contextPath>
     </webAppConfig>
     <connectors>
     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
       <port>8080</port>
     </connector>
     <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
       <port>8443</port>
       <keystore>${project.basedir}/localhost.keystore</keystore>
       <password>123456</password>
       <keyPassword>123456</keyPassword>
     </connector>
     </connectors>
   </configuration>
</plugin>

 

  

示例源代码:https://github.com/zhangkaitao/shiro-example;可加群 231889722 探讨Spring/Shiro技术。

  

请你欣赏春天美景 http://user.qzone.qq.com/314154083/photo/V10a4ot72FxYVR?ptlang=2052


 

猜你喜欢

转载自jinnianshilongnian.iteye.com/blog/2036420