欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sql注入的demo

程序员文章站 2023-12-22 11:47:52
...

今天研究了一下sql注入,废话不多说,直接上代码;
UserMapper

package com.liuzm.mapper;

import com.liuzm.bean.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
  
    @Select("select * from user_table where id = ${id}")
    User selectUser(String id);

    @Select("select * from user_table where id = #{id}")
    User selectUser2(String id);
}

Test测试类

package com.liuzm;

import com.liuzm.mapper.UserMapper;
import com.liuzm.service.impl.UserServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes=MybatisApplication.class)
public class SqlTest {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserMapper userMapper;

    @Test
    public void test(){
        userService.DeleteUserById("1");
    }

    @Test
    public void test2(){
        userMapper.selectUser("1 or 1 = 1");
    }


    @Test
    public void test3(){
        userMapper.selectUser2("1");
    }
}

控制台打印

#在application.properties文件中加上这个sql语句输出打印到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#{}效果图
sql注入的demo
${}效果图(请忽略报错)
sql注入的demo

可以看出有明显的不同:
#{id}在经过预编后相当于问号(?),可以认为你传过去的就是一个整体值;
${id}传来是什么就是什么,我们传过去的是1 or 1 = 1 ,sql语句也就是1 or 1 = 1;有可能会改变原有的sql语句;
所以尽量使用#{};

相关标签: sql

上一篇:

下一篇: