Spring Security身份认证之HelloSpringSecurity(附源码)(3)

<?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/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 
    <mvc:annotation-driven></mvc:annotation-driven>
     
    <context:component-scan base-package="com.favccxx.favsecurity.web"></context:component-scan>
     
    <bean
       >
        <property
            value="org.springframework.web.servlet.view.JstlView" />
        <property value="/WEB-INF/views" />
        <property value=".jsp" />
    </bean>
 
</beans>

6. 新建HelloSpringSecurityController.java文件,代码如下:

package com.favccxx.favsecurity.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloSpringSecurityController {
     
    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "Welcome - Spring Security Hello World");
        mav.addObject("message", "This is welcome page!");
        mav.setViewName("/hello");
        return mav;
    }
     
    @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
    public ModelAndView welcome() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "Welcome - Spring Security Hello World");
        mav.addObject("message", "This is welcome page!");
        mav.setViewName("/hello");
        return mav;
    }
     
    @RequestMapping(value = "/admin", method = RequestMethod.GET)
          public ModelAndView admin() {
       
              ModelAndView mav = new ModelAndView();
              mav.addObject("title", "Admin - Spring Security Hello World");
              mav.addObject("message", "This is protected page!");
              mav.setViewName("/admin");
            return mav;
     
        }
     
    }
 
}

7. 在/WEB-INF/views文件夹下分别创建admin.jsp和hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>   
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
<body>
    <h1>Title : ${title}</h1>
    <h1>Message : ${message}</h1>
    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <h2>
            Welcome : ${pageContext.request.userPrincipal.name} | <a href="<c:url value="/j_spring_security_logout" />"> Logout</a>
        </h2>
    </c:if>   
</body>
</html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/844f41c5fecb7d7458581646031b4c2c.html