Security自定义逻辑认证(极简案例)

项目结构

在这里插入图片描述

config

SecurityConfig

package com.wunaiieq.tmp2024121105.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
//security的配置类
@Configuration
public class SecurityConfig {
    //密码默认加密,此处配置密码编码器,设置不解析密码
    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
}

controller

没用过,sercurity会自动联网,使用一个登录页面
PageController

package com.wunaiieq.tmp2024121105.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
}

entity

Users

package com.wunaiieq.tmp2024121105.entity;

public class Users {
    private Integer id;
    private String username;
    private String password;
    private String phone;

    public Users() {
    }

    public Users(Integer id, String username, String password, String phone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

mapper

UsersMapper

package com.wunaiieq.tmp2024121105.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wunaiieq.tmp2024121105.entity.Users;

public interface UsersMapper extends BaseMapper<Users> {
}

service

UserDetailService01

package com.wunaiieq.tmp2024121105.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wunaiieq.tmp2024121105.entity.Users;
import com.wunaiieq.tmp2024121105.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.stereotype.Service;

@Service
public class UserDetailService01 implements UserDetailsService {
    @Autowired
    private UsersMapper usersMapper;
    //自定义认证逻辑
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.查询条件
        QueryWrapper<Users> wrapper = new QueryWrapper<Users>().eq("username", username);
        //2.查询用户
        Users users =usersMapper.selectOne(wrapper);
        //3.封装为UserDetails对象
        UserDetails userDetails = User.withUsername(users.getUsername())
                .password(users.getPassword())
                .authorities("admin")
                .build();
        //4.返回封装好的UserDetails对象
        return userDetails;
    }
}

启动类

package com.wunaiieq.tmp2024121105;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.wunaiieq.tmp2024121105.mapper")
public class Tmp2024121105Application {

    public static void main(String[] args) {
        SpringApplication.run(Tmp2024121105Application.class, args);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无奈ieq

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

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

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

打赏作者

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

抵扣说明:

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

余额充值