0%

shiro-web

servlet+jsp+shiro

shiro-web

  1. 新建模块

    image-20200926131453085

  2. 导入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
    <artifactId>shiro-learn</artifactId>
    <groupId>com.xiaoruiit</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>chapter4-web</artifactId>

    <packaging>war</packaging>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.9</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
    </dependency>
    <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.2</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.2.2</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.25</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>0.2.23</version>
    </dependency>
    </dependencies>

    <build>
    <finalName>chapter4-web</finalName>
    <plugins>
    <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>
    </configuration>
    </plugin>

    <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>
    </project>
  3. 配置web.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"
    metadata-complete="false">

    <!--- shiro 1.2 -->
    <listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>shiroEnvironmentClass</param-name>
    <param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value><!-- 默认先从/WEB-INF/shiro.ini,如果没有找classpath:shiro.ini -->
    </context-param>
    <context-param>
    <param-name>shiroConfigLocations</param-name>
    <param-value>classpath:shiro-web.ini</param-value>
    </context-param>
    <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--<error-page>-->
    <!--<error-code>401</error-code>-->
    <!--<location>/WEB-INF/jsp/unauthorized.jsp</location>-->
    <!--</error-page>-->

    </web-app>
  4. 配置web环境

    image-20200926193310648

  5. 配置shiro-web.ini

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    [main]
    #默认是/login.jsp,配置访问servleturl
    authc.loginUrl=/login
    roles.unauthorizedUrl=/unauthorized
    perms.unauthorizedUrl=/unauthorized

    logout.redirectUrl=/login

    [users]
    zhang=123,admin
    wang=123

    [roles]
    admin=user:*,menu:*

    [urls]
    /logout2=logout
    /login=anon
    /logout=anon
    /unauthorized=anon
    /static/**=anon
    # 需要认证通过
    /authenticated=authc
    # 需要认证通过,并拥有admin角色
    /role=authc,roles[admin]
    # 需要认证通过,并拥有user下的create权限
    /permission=authc,perms["user:create"]

  6. 编写Servlet

    LoginServlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    package com.xiaoruiit.shiro.userServlet;

    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.IncorrectCredentialsException;
    import org.apache.shiro.authc.UnknownAccountException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.subject.Subject;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author hxr
    * @Classname LoginServlet
    * @Description ToDo
    */
    @WebServlet(name = "loginServelt", urlPatterns = "/login")
    public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String error = null;
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    try {
    subject.login(token);
    } catch (UnknownAccountException e) {
    error = "用户名/密码错误";
    } catch (IncorrectCredentialsException e) {
    error = "用户名/密码错误";
    } catch (AuthenticationException e) {
    //其他错误,比如锁定,如果想单独处理请单独catch处理
    error = "其他错误:" + e.getMessage();
    }
    if (error != null) {//出错了,返回登录页面
    req.setAttribute("error", error);
    req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp);
    } else {//登录成功
    req.getRequestDispatcher("/WEB-INF/jsp/loginSuccess.jsp").forward(req, resp);
    }
    }
    }

    LogoutServlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    package com.xiaoruiit.shiro.userServlet;

    import org.apache.shiro.SecurityUtils;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author hxr
    * @Classname LogoutServlet
    * @Description ToDo
    */
    @WebServlet(name = "logoutServlet", urlPatterns = "/logout")
    public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    SecurityUtils.getSubject().logout();
    req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }
    }
  7. 编写jsp

    login.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>登录</title>
    <style>.error{color:red;}</style>
    </head>
    <body>

    <div class="error">${error}</div>
    <form action="${pageContext.request.contextPath}/login" method="post">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit" value="登录">
    </form>

    </body>
    </html>

    loginSuccess.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    欢迎${subject.principal}登录成功!<a href="${pageContext.request.contextPath}/logout">退出</a>
    </body>
    </html>

    index.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>首页</title>
    </head>
    <body>

    <a href="${pageContext.request.contextPath}/login">登录</a><br/>
    <a href="${pageContext.request.contextPath}/authenticated">已身份认证</a><br/>
    <a href="${pageContext.request.contextPath}/role">角色授权</a><br/>
    <a href="${pageContext.request.contextPath}/permission">权限授权</a><br/>

    </body>
    </html>
  8. 启动

    先配置maven环境

    image-20200926131907024

    image-20200926131944076

  9. 测试

    image-20200926131739585image-20200926131755401