《Java后端安全开发Spring Security开发REST服务》4章-3——记住我

BrowserSecurityConfig

	@Bean
	public PersistentTokenRepository persistentTokenRepository() {
		JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
		tokenRepository.setDataSource(dataSource);
		//启动时建表,下次启动服务记得关掉
		//tokenRepository.setCreateTableOnStartup(true);
		return tokenRepository;
	}


	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		/*
		http.formLogin()	//表单登录
			.loginPage("/imooc-signIn.html")	//指定登录页面,替代spring security默认页面
			.loginProcessingUrl("/authentication/form")		//对应form表单中的action,UsernamePasswordAuthenticationFilter将处理此URL对应的请求
			//http.httpBasic()	//http登录对话框
			.and()
			.authorizeRequests()
			.antMatchers("/imooc-signIn.html").permitAll()	//别忘了,不然会循环跳转
			//.antMatchers("/authentication/form").permitAll()	//别忘了,不然会循环跳转
			.anyRequest()
			.authenticated()
			.and()
			.csrf().disable();
		*/
		/*
		http.formLogin()	//表单登录
		.loginPage("/authentication/require")	//跳转到BrowserSecurityController对应路径
		.loginProcessingUrl("/authentication/form")		//对应form表单中的action,UsernamePasswordAuthenticationFilter将处理此URL对应的请求
		//http.httpBasic()	//http登录对话框
		.and()
		.authorizeRequests()
		.antMatchers("/authentication/require").permitAll()	//别忘了,不然会循环跳转
		.anyRequest()
		.authenticated()
		.and()
		.csrf().disable();
		*/
		
		//创建并验证码过滤器ValidateCodeFilter,并把它加到UsernamePasswordAuthenticationFilter前面
		//ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
		//validateCodeFilter.setAuthenticationFailureHandler(imoocAuthenticationFailureHandler);
		//validateCodeFilter.setSecurityProperties(securityProperties);
		//validateCodeFilter.afterPropertiesSet();
		
		http
		//把ValidateCodeFilter插入到UsernamePasswordAuthenticationFilter前面
		.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
		.formLogin()	//表单登录
		.loginPage("/authentication/require")	//跳转到BrowserSecurityController对应路径
		.loginProcessingUrl("/authentication/form")		//对应form表单中的action,UsernamePasswordAuthenticationFilter将处理此URL对应的请求
		.loginProcessingUrl("/authentication/mobile")	
		//http.httpBasic()	//http登录对话框
		.successHandler(imoocAuthenticationSuccessHandler)	//验证成功后调用此处理器
		.failureHandler(imoocAuthenticationFailureHandler)  //验证失败后调用此处理器
		//remeberMe配置
		.and()
		.rememberMe()
		.tokenRepository(persistentTokenRepository())
		.tokenValiditySeconds(securityProperties.getBrowser().getRemmberMeSeconds())
		.userDetailsService(userDetailsService)
		//验证码配置
		.and()
		.authorizeRequests()
		.antMatchers("/authentication/require",
				SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,	//authentication/mobile
				securityProperties.getBrowser().getLoginPage(),
				"/error",
				"/code/*"	//验证码图片
				).permitAll()	//别忘了,不然会循环跳转
		//此处securityProperties.getBrowser().getLoginPage()读出的就是wxm-spring-security-demo//main/java/resources/application.properties中的"imooc.security.browser.loginPage = /demo-signIn.html"
		//.antMatchers("/authentication/form").permitAll()	//别忘了,不然会循环跳转
		.anyRequest()
		.authenticated()
		.and()
		.csrf().disable();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值