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

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


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

分析需求:

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

在这里插入图片描述

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

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

在这里插入图片描述

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

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

springsecurity整合

1.导入依赖

 <!--SpringSecurity-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.0.1.RELEASE</version>
        </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客户管理系统LOGIN</h2>
    <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时多写点东西罢了!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结构化思维wz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值