springboot security学习笔记【自定义登录验证方法和登录界面】

最近在学习spring boot,想要自己做一个项目,然后就去研究权限验证的框架,找来找去,也就springboot security能看得懂一些,但是有个问题,框架里边自己提供了登录页面,提供了登录验证,网上找的一堆资料,讲得都云里雾里,花了一天的时间一点点的摸索,最后终于是成功了。

自定义登录页面和登录成功失败的处理

需要定义一个类继承 WebSecurityConfigurerAdapter类,并且需要重写configure(HttpSecurity http)

@Override
	protected void configure(HttpSecurity http) throws Exception{
		http.formLogin()
			//设置登录页面
			.loginPage("/toLogin")
			//设置处理登录的链接 就是指表单要向哪里提交并不是指你可以写个controller来响应登录
			.loginProcessingUrl("/login")
			//设置登录成功时的响应,我这里是用ajax提交的登录请求,所以用这样的方式来响应
			//也可以使用defaultSuccessUrl(defaultSuccessUrl)来设置登录成功之后跳转的页面
			.successHandler(new AuthenticationSuccessHandler() {
				@Override
				public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {
					PrintWriter out = response.getWriter();
					out.write("{\"success\":true}");
					out.flush();
					out.close();
				}
			})
			//设置登录失败之后的操作,failureUrl(authenticationFailureUrl)设置登录失败之后的跳转链接
			//可以设置一个controller来响应登录失败操作不设置响应默认会跳转登录界面然后附带参数
			//例如:/toLogn?error /toLogin?success 注意是loginPage方法设置的链接,不是处理登录的链接
			.failureHandler(new AuthenticationFailureHandler() {
				@Override
				public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
						AuthenticationException exception) throws IOException, ServletException {
					PrintWriter out = response.getWriter();
					out.write("{\"success\":true,\"用户名或密码错误\"}");
					out.flush();
					out.close();
				}
			})
			//允许所有人访问,使用这个方法使得前面设置的链接允许访问,否则会重定向过多
			.permitAll()	
			//进入下一项内容配置,目的是跳转到原始的类里边				
			.and()
			//开启过滤设置
			.authorizeRequests()	
			//设置允许访问的路径,我这里是设置了允许访问静态文件存放路径下的所有资源		
			.antMatchers("/static/**").permitAll()
			//除以上设置的路径以外的所有路径
			.anyRequest()
			//登录之后才允许访问
			.authenticated()
			//关闭csrf据说是为了防止表单提交失败,具体有没有影响我也没有测试
			.and().csrf().disable();
	}

定义的类上面要写 @Configuration

自定义登录验证方法

登录验证方法需要重写 configure(AuthenticationManagerBuilder auth)
注意,参数类型不一样,是上面那个方法的重载

@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception{
		//设置密码验证方法
		auth.userDetailsService(new UserDetailsService() {
			@Override
			public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
				//这里写一个根据用户名获取密码的方法,我这里写了一个固定的密码,然后使用
				//BCryptPasswordEncoder进行加密,是springboot security提供的一个密码加密方法
				//不管密码的长度是多少,最后会返回一个长度为60的字符串
				String pass = new BCryptPasswordEncoder().encode("1234567");
				//返回一个user类,把获取到的密码连同用户名一起放进去,最后一个参数应该是用户的角色信息
				return new User(username,pass,AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
			}})
			//设置用户密码的加密方式,直接使用BCryptPasswordEncoder类就可以了,不用自己重新
			.passwordEncoder(new BCryptPasswordEncoder());
	}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值