zoukankan      html  css  js  c++  java
  • Spring Security 基础登录实例

    1 新建Java Web项目

    导入Jar:
    这里写图片描述

    2 修改web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    springSecurityFilterChain这个filter定义之后spring security保护web请求这个作用就开始生效了,spring security将会对请求的url进行拦截并判断其权限。

    3 新建springmvc-servlet.xml

    <?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:security="http://www.springframework.org/schema/security"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context-4.0.xsd
                               http://www.springframework.org/schema/mvc 
                               http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                               http://www.springframework.org/schema/security 
                               http://www.springframework.org/schema/security/spring-security-4.1.xsd">
    
        <security:user-service id="userService">
            <security:user name="admin" password="admin"
                authorities="ROLE_USER,ROLE_ADMIN" />
            <security:user name="duruiqi" password="duruiqi"
                authorities="ROLE_USER" />
        </security:user-service>
    
        <security:authentication-manager>
            <security:authentication-provider
                user-service-ref="userService" />
        </security:authentication-manager>
    
        <security:http pattern="/favicon.ico" security="none" />
    
        <security:http auto-config="true">
            <security:intercept-url pattern="/**"
                access="hasRole('ROLE_USER')" />
        </security:http>
    
        <context:component-scan base-package="cn.zifangsky.* *.controller" />
    
        <context:annotation-config />  <!-- 激活Bean中定义的注解 -->
        <mvc:annotation-driven />
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    1 SpringMVC配置不过多解释 不懂 出门左转 百度

    2

    <security:http auto-config="true">
            <security:intercept-url pattern="/**"
                access="hasRole('ROLE_USER')" />
        </security:http>

    “http”定义了一个Web相关的权限配置
    “intercept-url”标签则配置了权限控制规则,即:网站根目录下的所有目录都需要有“ROLE_USER”的权限才允许访问。
    简单的说在访问首页的index.jsp文件都需要进行权限验证,也就是说程序运行之后就需要登录验证,验证通过之后才能访问首页及其他页面。
    当然,这个登录页面是spring security默认自带的, 当指定http元素的auto-config=”true”时,就相当于如下内容的简写。

    <security:http>
          <security:form-login/>
          <security:http-basic/>
          <security:logout/>
       </security:http>
       这些元素负责建立表单登录、基本的认证和登出处理。它们都可以通过指定对应的属性来改变它们的行为。
    

    **3**spring security需要验证访客的身份,那么就需要我们提供哪些用户具有哪些访问权限,配置“authentication-manager”了,当然真正进行身份验证的是“authentication-provider”这个元素,从上面代码可以看出这里只是简单引用了一个采用硬编码的“user-service”,其里面定义了两个角色以及它们所对应的权限。类似shiro的使用。

    4 index.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <base href="<%=basePath%>">
        <title>Spring Security Demo</title>
    </head>
    <body>
        <div align="center">
            Hello Spring Security!
        </div>
    </body>
    </html>

    5 项目运行

    这里写图片描述

    本文借鉴好友的一个文章,有感而来,传送门:http://www.zifangsky.cn/570.html

    【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


    —–乐于分享,共同进步!
    —–更多文章请看:http://blog.csdn.net/duruiqi_fx


  • 相关阅读:
    实现一个程序两套快捷键
    SystemC中文教程一
    logback的使用和logback.xml详解
    mysql语句练习50题
    Intellij IDEA中使用Debug调试详解
    用node-webkit把web应用打包成桌面应用
    Idea导入项目详解
    iReport 5.6.0 Error: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : data 最优解决方案
    CentOS 7.X 关闭防火墙
    将 MySQL root 的远程访问密码由空密码改为 password
  • 原文地址:https://www.cnblogs.com/hainange/p/6153802.html
Copyright © 2011-2022 走看看