SpringSecurity自定义实现

首先SpringSecurity在无自定义是默认是用户名user,和随机密码

·自定义实现(有三种方法)

①通过配置文件定义:

  首先创建一个SecurityProperties类,使用注解@ConfigurationPropertie(prefix="userinfo")(这里的prefix属性指的是spring.yml中的属性)

@ConfigurationProperties(prefix="userinfo")
public class SecurityProperties(){
}

  然后我们在application.yml中配置

userinfo:
    name:banana
    password:123

  这样完成了通过配置文件定义用户名和密码

第二种方法:

②通过配置类实现

继承WebSecurityConfigurerAdapter重写configure方法

具体实现:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
	protected void configure(HttpSecurity http) throws Exception {
        //授权认证
		http.authorizeRequests()
				//error.html不需要被认证
				// .antMatchers("/error.html").permitAll()
				.antMatchers("/error.html").access("permitAll()")
				//login.html不需要被认证
				// .antMatchers("/login.html").permitAll()
				.antMatchers("/showLogin").access("permitAll()")
				.antMatchers("/js/**","/css/**","/images/**").permitAll()
				// .antMatchers("/**/*.png").permitAll()
				//正则表达式匹配
				// .regexMatchers(".+[.]png").permitAll()
				// .regexMatchers(HttpMethod.GET,"/demo").permitAll()
				//mvc匹配servletPath为特有方法,其他2种匹配方式没有
				// .mvcMatchers("/demo").servletPath("/xxxx").permitAll()
				//和mvc匹配等效
				// .antMatchers("/xxxx/demo").permitAll()
				//权限判断
				// .antMatchers("/main1.html").hasAuthority("admiN")
				// .antMatchers("/main1.html").hasAnyAuthority("admin","admiN")
				//角色判断
				// .antMatchers("/main1.html").hasRole("abC")
				// .antMatchers("/main1.html").access("hasRole('abc')")
				// .antMatchers("/main1.html").hasAnyRole("abC,abc")
				//IP地址判断
				// .antMatchers("/main1.html").hasIpAddress("127.0.0.1")
				//所有请求都必须被认证,必须登录之后被访问
				.anyRequest().authenticated()
    }

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

③通过自定义配置类

实现UserDetails接口

官方推荐加密方案 BCryptPasswordEncoder(BCryptPasswordEncoder为PasswordEncoder的实现)

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private PasswordEncoder pw;

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

		System.out.println("执行了loadUserByUsername方法");

		//1.查询数据库判断用户名是否存在,如果不存在就会抛出UsernameNotFoundException异常
		if (!"admin".equals(username)){
			throw new UsernameNotFoundException("用户名不存在!");
		}
		//2.把查询出来的密码(注册时已经加密过)进行解析,或者直接把密码放入构造方法
		String password = pw.encode("123");
		return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc," +
				"/main.html,/insert,/delete"));
	}
}

这里重点提一下BCryptPasswordEncoder,SpringSecurity中的BCryptPasswordEncoder采用SHA+随机盐+密钥对密码进行加密,SHA是Hash算法,不是加密算法,使用加密算法意味着可以解密(但是Hash处理,过程是不可逆的)

PasswordEncoder主要方法:

        1.encode(password):进行加密

        2.matches(password,passwordHash):对密码和数据库内的密码(都是加密后)进行对比,返回布尔值判断数值是否一致。

        3.upgradeEncoding:再次加密(一般用不上)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值