Security实现登录安全控制

1:在pom.xml中添加依赖

<!-- 身份验证 -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>

2:在web.xml中添加配置

注意:

classpath:spring/spring-security.xml写的是自己配置sping-security的路径
<!--登录校验-->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/spring-security.xml</param-value>
</context-param>
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
<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>

3:在配置文件中配置spring-security.xml

我配置在resources下的spring文件夹下

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans 
    xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
    <!-- 不拦截页面 -->
    <http pattern="/login.html" security="none"/>
    <http pattern="/css/**" security="none"/>
    <http pattern="/img/**" security="none"/>
    <http pattern="/js/**" security="none"/>
    <http pattern="/plugins/**" security="none"/>
    
    <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
    <http use-expressions="false">
        <!-- 
            配置SpringSecurity的拦截路径(拦截规则) 
            * pattern:配置拦截规则。   /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
            * access:设置角色  角色命名 ROLE_角色名称  如:  ROLE_USER  
        -->
        <intercept-url pattern="/**" access="ROLE_ADMIN"/>
        
        <!-- 
        开启表单验证 
            username-parameter="username" 
            password-parameter="password" 
            login-page            :登录页面名称  以  / 开始
            default-target-url    :登录成功后跳转的页面
            login-processing-url:提交的路径的设置 默认值"/login" 可以修改
        -->
        <form-login login-page="/login.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/login.html"/>
        <!-- 不使用csrf的校验 -->
        <csrf disabled="true"/>
        <!-- 配置框架页面不拦截 -->
        <headers>
            <frame-options policy="SAMEORIGIN"/>
        </headers>
        <!-- 注销的配置,这里登录可以先不写 -->
        <logout logout-url="/logout" logout-success-url="/login.html" />
    </http>
    <!-- 配置认证管理器 -->
    <authentication-manager>
        <!-- 认证的提供者 -->
        <authentication-provider>
            <user-service>
          <!-- 可以用来登录的用户名和密码 --> <user name="admin" password="123456" authorities="ROLE_ADMIN"/> <user name="wc" password="123456" authorities="ROLE_ADMIN"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>

4:登录页面设置及注意事项

注意:表单的action必须为/login 请求方式必须为post请求

用户名和密码输入框的name值必须分别设置为username和password

点击超链接提交表单onclick="document:loginform.submit()"

<form id="loginform" action="/login" method="post">
    <input name="username" type="text" placeholder="邮箱/用户名/手机号">
    <input name="password" type="password" placeholder="请输入密码">
    <a onclick="document:loginform.submit()"  target="_blank">登&nbsp;&nbsp;录</a>
</form>
原文地址:https://www.cnblogs.com/wangju/p/11857046.html