简介

  • Secure Sockets Layer,安全套接层。在传输层对网络进行加密,为网络通信安全提供安全及数据完整性。
  • 利用数据加密技术,确保数据在传输过程中不会被截取及窃听。
  • 广泛应用于web浏览器与服务器之间的身份认证和加密数据传输。
  • 对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,是则自动重定向到https进行访问。

流程

  • 首先生成数字证书,保存到D:/localhost.keystore。证书生成步骤如下:
    • 使用jdk的KeyTool命令,生成证书(包含证书、公钥、私钥)到D:/localhost.keystore
keytool -genkey -keystore "D:\localhost.keystore" -alias localhost -keyalg RSA
输入密钥库口令:123456
再次输入新口令:123456
您的名字与姓氏是什么?
  [Unknown]:  localhost //必须是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> 的密钥口令
        (如果和密钥库口令相同, 按回车):回车
再次输入新口令:
  • 可以选择导出cer证书并安装到浏览器内,详见笔记:https、SSL和TLS,使用KeyTool生成自签名证书。不过不安装也没事,只是进行ssl访问时会提示网站不安全而已。

  • 然后设置tomcat/config/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时的口令;
        clientAuth:设置是否双向验证,默认false,表示只有客户端验证服务器,
        服务器不验证客户端;-->
    
  • 添加SSL到配置文件spring-shiro-web.xml,使用和十三章一样的代码。

    <bean id="sslFilter" class="org.apache.shiro.web.filter.authz.SslFilter">
        <property name="port" value="8443"/> <!--SslFilter端口默认是443,此处使用了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 //表示访问登录页面时需要走SSL
                /logout = logout
                /authenticated.jsp = authc
                /** = user
            </value>
        </property>
    </bean>
    
  • 打包并部署到tomcat:否则直接运行是没用的。

    <build>
        <finalName>chapter14</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <path>/${project.build.finalName}</path>
            </configuration>
          </plugin>
        </plugins>
    </build>
    
  • 运行mvn package,把打成的war包复制到tomcat/webapps下,运行tomcat/bin/startup.bat,访问localhost:8080/chapter14/,自动跳转到https://localhost:8080/chapter14/login.jsp。

  • 《跟我学Shiro》第十四章

  • 代码实例:ideaProjects/shiro-example-chapter14(是从作者github拷下来的代码,但是所有文件都换成了shirochapter12的;和shirochapter12完全一样,包括依赖版本、resources下各xml和web.xml都比对一致;除了pom.xml中顶上url,但经测试无论用哪个url都对,但不知为何14没问题,12在tomcat运行却一直报错如下:
org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'shiroFilter' defined in class path resource
  [spring-shiro-web.xml]: Initialization of bean failed; 
  nested exception is org.springframework.beans.ConversionNotSupportedException:
  Failed to convert property value of type [java.util.LinkedHashMap] to required type
  [java.util.Map] for property 'filters'; 
  nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.servlet.Filter] for property
  'filters[ssl]': no matching editors or conversion strategy found