ITeye博客

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

jinnianshilongnian · 2014-03-25 15:38 · 跟我学Shiro · 企业架构 · Markdown

目录贴: 跟我学Shiro目录贴

 

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

 

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

使用JDK的keytool命令,生成证书(包含证书/公钥/私钥)到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

评论(14)

  1. #14 楼 · 今晚吃肉 · 2018-01-05 16:26

    tao哥(不能打汉字 :x ),我参照你这样的配置后,直接打个war包放在tomcat上启动后连接无法访问,给出的信息如下

    05-Jan-2018 16:22:54.638 INFO [http-apr-8443-exec-2] org.apache.coyote.http11.AbstractHttp11Processor.process Error parsing HTTP request header
     Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
    2018-01-05 16:22:54.785 [http-apr-8443-exec-4] WARN  org.springframework.web.servlet.PageNotFound:1205 - No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'springServlet'
    

    我在tomcat中的配置为

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

    在shiro的配置文件中的配置为

    <!-- SSL配置 -->
        <bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter">
            <!-- 要和tomcat中配置的一样 -->
            <property name="port" value="8443" />
        </bean>
    

    ,这是为什么呢

  2. #13 楼 · johnmann · 2016-08-11 16:07

    您的名字与姓氏是什么? : localhost

    生成证书的时候特别注意一定要“名字与姓氏”用localhost,要不然在15章节里面会会有错误。参考:http://java.globinch.com/enterprise-java/security/fix-java-security-certificate-exception-no-matching-localhost-found/

  3. #12 楼 · zain_xu · 2016-05-18 08:57

    很受用!!!

  4. #11 楼 · cherishit · 2014-08-06 22:25

    请问楼主 , 如果单向认证https 这样第一次访问需要添加可信站点 . 这个要怎么破 ? 这个是因为证书没有经过机构颁发吗?

  5. #10 楼 · Long_yuan · 2014-05-16 16:12

    tomcat 开启了ssl会受 Heartbleed影响么?

  6. #9 楼 · Long_yuan · 2014-05-16 15:11

    能把tomcat配置全文 贴出来么

  7. #8 楼 · jinnianshilongnian · 2014-03-27 15:34

    inotgaoshou writes:

    [quote=jinnianshilongnian][quote=inotgaoshou] :idea: tao哥结婚了,恭喜恭喜~[/quote] 从哪里看到我结婚了 :oops: 木有啊 [/quote] :oops: tao哥,不好意思看错了,你好像是伴郎~ http://user.qzone.qq.com/314154083/photo/V10a4ot72FxYVR?ptlang=2052

    哈哈

  8. #7 楼 · inotgaoshou · 2014-03-27 14:07

    jinnianshilongnian writes:

    [quote=inotgaoshou] :idea: tao哥结婚了,恭喜恭喜~[/quote] 从哪里看到我结婚了 :oops: 木有啊

    :oops: tao哥,不好意思看错了,你好像是伴郎~ http://user.qzone.qq.com/314154083/photo/V10a4ot72FxYVR?ptlang=2052

  9. #6 楼 · jinnianshilongnian · 2014-03-26 13:30

    zqb666kkk writes:

    我总感觉 涛 哥 写的很空泛

    那应该如何写? 请指点。 :D

  10. #5 楼 · zqb666kkk · 2014-03-26 13:13

    我总感觉 涛 哥 写的很空泛

  11. #4 楼 · jinnianshilongnian · 2014-03-26 08:14

    amberlike writes:

    谢谢涛_哥的无私奉献,您的代码很让人感动,尤其是详细的注释

    :D

  12. #3 楼 · amberlike · 2014-03-25 23:14

    谢谢涛_哥的无私奉献,您的代码很让人感动,尤其是详细的注释

  13. #2 楼 · jinnianshilongnian · 2014-03-25 17:47

    inotgaoshou writes:

    :idea: tao哥结婚了,恭喜恭喜~

    从哪里看到我结婚了 :oops: 木有啊

  14. #1 楼 · inotgaoshou · 2014-03-25 17:12

    :idea: tao哥结婚了,恭喜恭喜~

评论功能维护中,当前仅展示历史评论。