SpringSecurity 自定义登录用户名和密码,java代码在内存中配置不起作用,怎么办???

自定义的username 为  admin;password  为 123;

可是就是不起作用。

UserDetailServiceImpl  

package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
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.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: andyf
 * @Date: 2021/4/6
 * @Time: 19:08
 * @Description: 自定义登录逻辑
 */

@Service
public class UserDetailServiceImpl implements UserDetailsService {


    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

//        测试  是否 走自定义登录的逻辑了
        System.out.println("执行了自定义登录逻辑!");

//        1 根据用户名 去数据库查询,如果不存在抛异常
        if (!"admin".equals(s)){
            throw new UsernameNotFoundException("用户名不存在!");

        }
//        2 比较密码 【注册时已经加密,】如果匹配成功返回UserDetails
//        由于暂时没有注册,所以先直接加密
        String password = passwordEncoder.encode("123");


        return new User(s,password, AuthorityUtils
                .commaSeparatedStringToAuthorityList("admin,normal"));

    }
}

配置类SecurityConfig

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: andyf
 * @Date: 2021/4/6
 * @Time: 19:06
 * @Description:
 */

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
//      表单提交
        http.formLogin()
//               自定义 登录界面
                .loginPage("/login.html")

//              必须和表单提交的 接口一样。会去执行自定义登录逻辑
                .loginProcessingUrl("/login")
//                登录成功后跳转的页面 ,只接受 post请求,不能使用 /main.html
                .successForwardUrl("/toMain");


//      授权,
        http.authorizeRequests()
//              但是,登录页面不需要拦截,要不然就无限循环了
                .antMatchers("/login.html").permitAll()
//               所有的请求都必须认证才能访问,必须登录
                .anyRequest().authenticated();


//      防火墙 关闭 。csrf防护
        http.csrf().disable();

    }

    @Bean
    public PasswordEncoder getPw(){
        return new BCryptPasswordEncoder();

    }

}

运行之后,还是会出现  默认的密码;

按照教程一步一步做的,就是 不起作用。

https://www.bilibili.com/video/BV1Av411b7go?p=8&spm_id_from=pageDriver

 

各位 ,有没有解决方法?

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Roam-G

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

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

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

打赏作者

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

抵扣说明:

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

余额充值