spring security 完成登录

1. 在manage-web工程引入依赖

    <!-- spring security -->
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
    </dependency>

 里面的jar包

 

2.login.html

 

 

 

 <form class="sui-form" action="/login" method="post" id="loginform">
       <div class="input-prepend"><span class="add-on loginname"></span>
       <input id="prependedInput" type="text" placeholder="邮箱/用户名/手机号" class="span2 input-xfat" name="username">
       </div>
     <div class="input-prepend"><span class="add-on loginpwd"></span>
        <input id="prependedInput" type="password" placeholder="请输入密码" class="span2 input-xfat" name="password">
     </div>
       <div class="setting">
          <div id="slider">
            <div id="slider_bg"></div>
               <span id="label">>></span> <span id="labelTip">拖动滑块验证</span>
             </div>
           </div>
    <div class="logined">
         <a class="sui-btn btn-block btn-xlarge btn-danger" href="admin/index.html" target="_blank" onclick="document:loginform.submit()">&nbsp;&nbsp;</a>
    </div>
   </form>

 

3.spring-security.xml

 

<?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">

    <!--定义默认不拦截的资源
        security="none" 设置页面不登陆也可以访问
        /*.html表示根目录下的html都可以访问,子目录不可以
    -->
    <http pattern="/*.html" security="none"></http>
    <http pattern="/css/**" security="none"></http>
    <http pattern="/img/**" security="none"></http>
    <http pattern="/js/**" security="none"></http>
    <http pattern="/plugins/**" security="none"></http>


    <!--页面拦截规则
        pattern="/*"  根目录下的资源 不包括子目录
        pattern="/**" 根目录下的资源 包括子目录
        角色:ROLE_XXX
        access="ROLE_USER" 表示当前用户必须有ROLE_USER的角色才可以访问根目录及其子目录
        use-expressions="false" 是否启用SPEL表达式 false为不启用 默认为true,
          如果不配置为false,access="hasRole(ROLE_USER)"
    -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_ADMIN"/>

        <!--开启表单登录功能  <form-login/>自动生成一个登录表单-->
        <!--<form-login/>-->
        <!--自定义登录表单配置
            login-page="/login.html"  登录表单
            default-target-url="/index.html"  登录成功后跳转到的页面
            authentication-failure-url="/login_err.html" 登录失败跳转的页面
            login-processing-url="/login2" 自定义访问路径  默认为login
            username-parameter="username" username是默认的 前端<input name="username"> 也可以修改
        -->
        <form-login login-page="/login.html" default-target-url="/admin/index.html" authentication-failure-url="/login.html"/>
        <!--关闭csrf跨域请求校验-->
        <csrf disabled="true"/>
        <!-- 允许使用框架页  因为brand.html 里面有一个index.html框架页 -->
        <headers>
            <frame-options policy="SAMEORIGIN"/>
        </headers>
        
    </http>

    <!--认证管理器-->
    <authentication-manager>
        <!--认证提供者-->
        <authentication-provider>
            <user-service>
                <!--配置当前系统的用户 这里手动添加用户名和密码
                    authorities="ROLE_USER"表示这个用户属于哪个角色
                -->
                <user name="admin" password="123456" authorities="ROLE_ADMIN"/>
                <user name="cmdzz" password="123456" authorities="ROLE_ADMIN"/>
            </user-service>
        </authentication-provider>

    </authentication-manager>
        
</beans:beans>

 

 

4.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_2_5.xsd"
    version="2.5">    
   <!-- 解决post乱码 -->
    <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>
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>    
    
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springmvc.xml</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- spring security 配置  start-->
        <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>
    <!-- spring security 配置  end-->
    
</web-app>

 

5.执行结果

    地址栏输入http://localhost:9101/   自动跳转到http://localhost:9101/login.html

    输入用户名和密码:登录成功 http://localhost:9101/admin/index.html

 

6.存在问题

先访问http://localhost:9101/admin/specification.html  跳转到http://localhost:9101/login.html

再输入用户名和密码登录时,发现不是跳到 http://localhost:9101/admin/index.html,而是跳到http://localhost:9101/admin/specification.html

 

7.解决  

在 spring-security.xml 配置文件中添加  always-use-default-target="true" 总是跳转到配置的页面

<form-login login-page="/login.html" default-target-url="/admin/index.html" authentication-failure-url="/login.html" always-use-default-target="true"/>
       

 

posted on 2019-06-21 14:59  成魔的蜘蛛  阅读(329)  评论(0)    收藏  举报

导航