我为了拒绝你直接访问这个地址我可以做一个登录处理,登录成功后可以访问

所以我们需要一个登录页面

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.chentianyu</groupId>
  6. <artifactId>SpringMVC_Login_Learn_One</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.11</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-webmvc</artifactId>
  24. <version>5.3.16</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet</groupId>
  28. <artifactId>javax.servlet-api</artifactId>
  29. <version>4.0.1</version>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <resources>
  34. <resource>
  35. <directory>src/main/java</directory>
  36. <includes>
  37. <include>**/*.xml</include>
  38. <include>**/*.properties</include>
  39. </includes>
  40. </resource>
  41. <resource>
  42. <directory>src/main/resources</directory>
  43. <includes>
  44. <include>**/*.xml</include>
  45. <include>**/*.properties</include>
  46. </includes>
  47. </resource>
  48. </resources>
  49. </build>
  50. </project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.chentianyu.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:default-servlet-handler />
    <mvc:annotation-driven />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index页面</title>
</head>
<body>
  <a href="${pageContext.request.contexPath}/showLogin">前往登录页</a>
</body>
</html>
package com.chentianyu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GoToAction {
    @RequestMapping("/showLogin")
    public String showLogin(){
        return "login";
    }

    //登录页面判断
    @RequestMapping("/login")
    public String login(String name,String password,HttpServletRequest request){
        //equalsIgnoreCase: 忽略大小写
        if("zar".equalsIgnoreCase(name)&& "123".equalsIgnoreCase(password)){
            return "main";
        }else {
            request.setAttribute("errMsg","用户名或密码不正确");
            return "login";
        }
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页</title>
</head>
<body>
<h2>登录</h2>
<p>${errMsg}</p>
<form action="${pageContext.request.contextPath}/login" >
    <input type="text" name="name" placeholder="姓名" /><br>
    <input type="password" name="password" placeholder="密码" /><br>
    <input type="submit" value="登录">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>main页面</h2>
    <p>也能说你输入的是指定的账号和密码</p>
</body>
</html>

执行效果
  • 用户名或密码不对

image.png
效果:
image.png

  • 用户名和密码都正确

image.png
效果:
image.png
所以我们可以上个拦截器的权限认定就可以实现