ssm项目BOOT使用springsecurity替换拦截器

BOOT管理系统使用springsecurity替换拦截器

目录

  • BOOT管理系统使用springsecurity替换拦截器
    • 分析需求:
    • springsecurity整合
      • 1.导入依赖
      • 2.整合
      • 3.说明
    • 效果:

之前学ssm做了一个小项目 boot_crm 客户管理系统,之前用的拦截器,这次用springsecurity完成基本功能。

分析需求:

之前我们使用拦截器配置的主要目的是拦截请求,防止用户未登录直接访问系统。配置后就是这种效果:

ssm项目BOOT使用springsecurity替换拦截器_第1张图片

当我们把拦截器去掉呢??

我们直接访问url:http://localhost:8080/customer/list

ssm项目BOOT使用springsecurity替换拦截器_第2张图片

可以发现,未登录也可以访问!!

所以们拦截器的主要功能就是让拦截请求,必须登录后才可以操作其他,而这个需求在springsecurity中简单的不得了!!

springsecurity整合

1.导入依赖

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

2.整合

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}

package com.wangze.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author: 王泽
 */

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //告诉系统密码不加密
    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
    @Override //配置http相关
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests() //开启配置
                .anyRequest().authenticated() //所有请求认证之后才可访问
                .and()
                .formLogin() //表单登录
                .loginProcessingUrl("/login") //处理登录请求的地址
                .loginPage("/index.jsp") //配置页
                .successForwardUrl("/customer/list")
                .usernameParameter("usercode")
                .passwordParameter("password")
                .permitAll() //与登录请求相关的都给过
                .and().csrf().disable();//关闭csrf供给策略方便测试

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("123").roles("admin")
        .and()
        .withUser("wangze").password("123").roles("admin")
        .and()
        .withUser("m0001").password("123").roles("admin");

    }

    
}

3.说明

我比较懒,看着完整的项目,实在不想去改数据库,写service,简简单单实现一下功能得了。希望大家不要跟我一样,如果想从数据库进行用户校验可以看我的前两篇文章,有mybatis的还有springdatajpa的!!

这个项目我直接把index页面改了:注意表单的接口/login ,还有name属性就好了!!

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>

<%--<jsp:forward page="/WEB-INF/jsp/login.jsp"/>--%>
DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BOOT客户管理系统title>
    <style>
        body {
            background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg') no-repeat;
            background-size: 100% 130%;
        }

        #login_box {
            width: 20%;
            height: 400px;
            background-color: #00000060;
            margin: auto;
            margin-top: 10%;
            text-align: center;
            border-radius: 10px;
            padding: 50px 50px;
        }

        h2 {
            color: #ffffff90;
            margin-top: 5%;
        }

        #input-box {
            margin-top: 5%;
        }

        span {
            color: #fff;
        }

        input {
            border: 0;
            width: 60%;
            font-size: 15px;
            color: #fff;
            background: transparent;
            border-bottom: 2px solid #fff;
            padding: 5px 10px;
            outline: none;
            margin-top: 10px;
        }

        button {
            margin-top: 50px;
            width: 60%;
            height: 30px;
            border-radius: 10px;
            border: 0;
            color: #fff;
            text-align: center;
            line-height: 30px;
            font-size: 15px;
            background-image: linear-gradient(to right, #30cfd0, #330867);
        }

        #sign_up {
            margin-top: 45%;
            margin-left: 60%;
        }

        a {
            color: #b94648;
        }
    style>
head>

<body>
<div id="login_box">
    <h2>BOOT客户管理系统LOGINh2>
    <form action="${pageContext.request.contextPath }/login" method="post" onsubmit="return check()">
    <div id="input_box">
        <input type="text" placeholder="请输入用户名" name="usercode" >
    div>
    <div class="input_box">
        <input type="password" placeholder="请输入密码" name="password">
    div>
    <button type="submit">登录button><br>
    form>
div>

body>

html>

效果:

如果不登录发送别的请求会自动跳转到登录界面,登录后默认跳转到/customer/list

如果时间充足,推荐还是改成从数据库校验的好,无非就是加个roles表,写实体类还有service时多写点东西罢了!

你可能感兴趣的:(项目,springsecurity,springsecurity,新星计划,ssm)