Spring Security 中There is no PasswordEncoder mapped for the id "null"问题解决方法和实现特定角色特定权限管理...

第一次使用Springboot权限管理Spring security时,使用 inMemoryAuthentication(内存)用户验证时,控制台报错:

原因分析:有些Spring security5.X版本没有提供PasswordEncoder实例,不是以明文的方式进行匹配,会报错。

解决:

1.创建PasswordEncoder的实现类MyPasswordEncoder.class:

package Encode;

import org.springframework.security.crypto.password.PasswordEncoder;

public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }
}

  2.在内存用户中添加passwordEncoder(new MyPasswordEncoder()):

package com.example.demo;

import Encode.MyPasswordEncoder;
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;

@Configuration
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter {

    //只有本小组的组员看到登陆,设置内存指定的登陆账号密码和角色
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //若不带.passwordEncoder(new MyPasswordEncoder())这样页面提交时就不是1以明文的方式进行匹配,报错
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN");
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("major").password("1234560").roles("USER");
    }

    //指定安全访问规则
    @Override
    protected  void configure(HttpSecurity http) throws Exception {
        //设置登录注销1,表单登陆不用拦截,其他需要
        http.authorizeRequests().antMatchers("/").permitAll()
        .anyRequest().authenticated()
        .and()
        .logout().permitAll()
        .and()
        .formLogin();
        //关闭csrf认证
        http.csrf().disable();
    }



    @Override
    public void configure(WebSecurity web) throws Exception{
        //设置静态资源不要拦截
        web.ignoring().antMatchers("js/**","/css/**","/images/**");
    }


}

另外,我的权限管理规则如下:

package com.example.demo;

import Encode.MyPasswordEncoder;
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;

@Configuration
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter {

    //只有本小组的组员看到登陆,设置内存指定的登陆账号密码和角色
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //若不带.passwordEncoder(new MyPasswordEncoder())这样页面提交时就不是1以明文的方式进行匹配,报错
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN");
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("major").password("1234560").roles("USER");
    }

    //指定安全访问规则
    @Override
    protected  void configure(HttpSecurity http) throws Exception {
        //设置登录注销1,表单登陆不用拦截,其他需要
        http.authorizeRequests().antMatchers("/").permitAll()
        .anyRequest().authenticated()
        .and()
        .logout().permitAll()
        .and()
        .formLogin();
        //关闭csrf认证
        http.csrf().disable();
    }



    @Override
    public void configure(WebSecurity web) throws Exception{
        //设置静态资源不要拦截
        web.ignoring().antMatchers("js/**","/css/**","/images/**");
    }


}

启动程序:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
//controller and rospondisty的组合
@RestController

//自动配置
@EnableAutoConfiguration


public class DemoApplication {

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

	@RequestMapping("/")
	public String say(){
		return "hello world";
	}

	@RequestMapping("/hello")
	public String hello(){
		return "hello";
	}


	//只有admin才可以使用这一级别
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping("/roleauth")
	public String hello01(){
		return "hello world!";
	}

}

3.运行

284702ae51e309c4bcd73892d2d43ba89ff.jpg

成功结果:

19d4e7797407fb3157e9dc95919649cf944.jpg

4.在规则配置中用户major是不能访问roleauth页面的,可代码中仍可:

68863d475c7793eca2b615abd5783febd98.jpg

815b63e74b332e95141619d8cff9bdc43f7.jpg

5.需要在项目启动程序中加入@EnableGlobalMethodSecurity(prePostEnabled = true)注解:

用户major无权访问,满足需求

9a30fe68a8b6669566f5c814f8b71376c3c.jpg

转载于:https://my.oschina.net/1024and1314/blog/3066305

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值