Spring Security入门Demo(IDEA工具)

Spring Security:

1.创建web工程

Spring Security入门Demo(IDEA工具)_第1张图片
Spring Security入门Demo(IDEA工具)_第2张图片
Spring Security入门Demo(IDEA工具)_第3张图片
Spring Security入门Demo(IDEA工具)_第4张图片
创建后工程目录
Spring Security入门Demo(IDEA工具)_第5张图片

2.在pom.xml添加坐标和插件

    <dependencies>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-webartifactId>
            <version>4.1.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-configartifactId>
            <version>4.1.0.RELEASEversion>
        dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <configuration>
                    
                    <port>9090port>
                    
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>
project>

3.修改web.xml


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

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-security.xmlparam-value>
  context-param>
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    listener-class>
  listener>

  <filter>
    <filter-name>springSecurityFilterChainfilter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
  filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChainfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

web-app>

4.在resouces目录下创建spring-security.xml


<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="/error.html" security="none"/>
    
    <http use-expressions="false">
        
        <intercept-url pattern="/**" access="ROLE_USER"/>
        
        
        <form-login login-page="/login.html"
                    always-use-default-target="true"
                    default-target-url="/index.jsp"
                    authentication-failure-url="/error.html"/>
        
        <csrf disabled="true"/>
    http>
    
    <authentication-manager>
        
        <authentication-provider>
            <user-service>
                
                <user name="admin" password="admin" authorities="ROLE_ADMIN"/>
                <user name="user" password="user" authorities="ROLE_USER"/>
            user-service>
        authentication-provider>
    authentication-manager>
beans:beans>

5.在webapp根目录下创建登录页面login.html和错误页面error.html

login.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录title>
head>
<body>
<form action="/login" method="post">
    <table align="center">
        <tr>
            <td>账号:td>
            <td><input type="text" name="username">td>
        tr>
        <tr>
            <td>密码:td>
            <td><input type="password" name="password">td>
        tr>
        <tr>
            <td colspan="2">
                <input style="width: 100%" type="submit" value="登录">
            td>
        tr>
    table>
form>
body>
html>

error.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆失败title>
head>
<body>
<h1>账号或密码错误!h1>
<a href="/login.html"><h3>回到登录页h3>a>
body>
html>

6.启动服务

Spring Security入门Demo(IDEA工具)_第6张图片

启动成功后在地址栏输入 http://localhost:9090/login.html
Spring Security入门Demo(IDEA工具)_第7张图片

7.测试

账号密码输入user登录成功
Spring Security入门Demo(IDEA工具)_第8张图片
账号密码输入admin时提示
Spring Security入门Demo(IDEA工具)_第9张图片
从地址栏可以看出我们已经登录成功,但是由于角色不同导致权限不够
用户名密码对,但是无权限访问,因为角色字符串不匹配资源
Spring Security入门Demo(IDEA工具)_第10张图片
Spring Security规则是必须账号密码正确且角色对应才能认证并授权成功

8.Spring Security自定义认证类,创建认证类

认证类需要实现UserDetailsService接口

package com.demo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;

public class UserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        //控制台打印用户输入的用户名
        System.out.println("输入的用户名" + name);
        //定义集合封装角色
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        //添加需要授权的角色 需要与配置规则对应
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        //返回User信息,需要三个参数:用户名,密码(一般是数据库中获取,这里只做模拟),授权的角色列表
        return new User(name, "123456", authorities);
        
    }
}

此认证类的意思是只要密码输入为123456就认证成功

9.修改spring-security.xml配置文件的认证管理器

需要将自定义认证类交给Spring管理

	
    <authentication-manager>
        
        <authentication-provider user-service-ref="userDetailsService">
            
        authentication-provider>
    authentication-manager>
    
    <beans:bean id="userDetailsService" class="com.demo.UserDetailsServiceImpl"/>

10.关闭服务,重新install

因为修改的配置已经添加的java代码的原因,需要重新install
Spring Security入门Demo(IDEA工具)_第11张图片

11.重启服务,并测试

Spring Security入门Demo(IDEA工具)_第12张图片
密码输入123456登录成功
Spring Security入门Demo(IDEA工具)_第13张图片

你可能感兴趣的:(框架)