Spring in action第四版向spring 5修正

使用WebMvcConfigurationSupport配置

原有的WebMvcConfigurerAdapter不可用

不要加@EnableWebMvc注解

否则addResourceHandlers不会运行,不能加载静态资源

properties文件

可以使用setDefaultEncoding保证UTF-8编码,防止国际化信息乱码

	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("file:///D:/EclipseApp/Spittr/src/messages");
		messageSource.setCacheSeconds(10);
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}

<spring:message>标签

	@Bean
	public MessageSource validationSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("file:///D:/EclipseApp/Spittr/src/ValidationMessages");
		messageSource.setCacheSeconds(10);
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}

	@Bean
	public LocalValidatorFactoryBean getValidator() {
		LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
		bean.setValidationMessageSource(validationSource());
		return bean;
	}

Thymeleaf的配置

使用ServletContextTemplateResolver来指定项目相对位置的模板文件,构造器参数是this.getServletContext()。

	@Bean
	public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
		viewResolver.setTemplateEngine(templateEngine);
		return viewResolver;
	}
	
	@Bean
	public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver) {
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver);
		return templateEngine;
	}
	
	@Bean
	public ServletContextTemplateResolver templateResolver() {
		ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext());
		templateResolver.setPrefix("/WEB-INF/templates/");
		templateResolver.setSuffix(".html");
		templateResolver.setTemplateMode("HTML5");
		return templateResolver;
	}

加入库的时候别把source和doc加进去,否则会报错。

Thymeleaf的编码


	@Bean
	public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
		viewResolver.setTemplateEngine(templateEngine);
		viewResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
		return viewResolver;
	}
	
	@Bean
	public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver) {
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver);
		return templateEngine;
	}
	
	@Bean
	public ServletContextTemplateResolver templateResolver() {
		ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext());
		templateResolver.setPrefix("/WEB-INF/templates/");
		templateResolver.setSuffix(".html");
		templateResolver.setTemplateMode("HTML5");
		templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
		return templateResolver;
	}

Spring Security相关

  1. conf目录在Eclipse工程列表Servers下的容器列表中,不是在原始的安装目录。
  2. server.xml文件中配置了https端口
  3. server.xml中配置的从http端口到https端口的重定向不起作用,应该在Spring Security的java配置中如下配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().antMatchers(HttpMethod.POST, "/spittles").hasRole("SPITTER")
		.anyRequest().permitAll()
		.and()
		.requiresChannel()
		.antMatchers("/spitter/register").requiresSecure();
		http.portMapper().http(80).mapsTo(8443);
	}
}
  1. Spring Security会自动打开CSRF防护,因此所有表单要加token,否则403!!!!!!!!!
  2. 自定义的登录页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spittr</title>
</head>
<body>
<h1>Welcome to Spittr</h1>
<form name='f' th:action='@{/login}' method='POST'>
	<table>
		<tr><td>User:</td><td><input type='text' name='username' value='' /></td></tr>
		<tr><td>Password:</td><td><input type='password' name='password' value='' /></td></tr>
		<tr><td colspan='2'><input type='submit' name='submit' value='Login' /></td></tr>
	</table>
</form>
</body>
</html>
	@Override
	protected void configure(HttpSecurity http) throws Exception {
//		http.csrf().disable();
		http.authorizeRequests()
		.antMatchers("/spittles").hasRole("USER")
		.anyRequest().permitAll()
//		.and()
//		.requiresChannel()
//		.antMatchers("/spitter/register").requiresSecure()
		.and().formLogin()
		.loginPage("/login").permitAll()
		;
//		http.portMapper().http(8080).mapsTo(8443);
	}
  1. Chrome对csrf的支持有些奇怪。。。其它浏览器都能正常运行,chrome报403
Spring是掠过Java大地的一阵清风。Spring是以反向控制设计原理为基础,无需EJB而功能依然强大的轻量级J2EE开发框架。Spring大大简化了使用接口开发的复杂性,并且加快和简化了应用系统的开发。使用简单JavaBean就可以得到EJB的强大功能。<br>本书介绍了Spring背后的原理,引领你迅速进入对框架的体验之中。结合简短代码片断和贯穿全书的持续示例,本书向你展示了如何创建简单有效的J2EE应用系统。你将看到如何使用先进的开源工具解决持久层问题,以及如何将你的应用系统与其他流行Web框架集成。你将学习如何使用Spring管理大量的基础设施代码,这样你就可以将注意力集中在真正的问题上——重要的业务需要。<br>本书内容:<br>·使用Hibernate、JDO、iBatis、OJB以及JDBC开发持久层;<br>·声明式事务与事务管理;<br>·与其他Web框架集成:Struts、WebWork、Tapestry、Velocity;<br>·访问J2EE服务,如JMS和EJB;<br>·使用AOP解决交叉问题;<br>·企业组应用系统最佳实践。<br>“……一种解释Spring中各个主题的很好途径……我喜欢这本书”<br>——Christian Parker,Adigio公司总裁<br>“……没有其他书籍可以与这本书的实用性相提并论。”<br>——Olivier Jolly,J2EE构架师,Interface SI<br>“我很喜欢这种展示Spring的方式。”<br>——Norman Richards,XDoclet in Action的作者之一<br>“我极力推荐这本书。”<br>——Jack Herrington,Code Generation in Action的作者 <br>----总共8部分rar下载完后解压 -----<br>Spring in Action. 中文版.part1.rar<br>Spring in Action. 中文版.part2.rar<br>Spring in Action. 中文版.part3.rar<br>Spring in Action. 中文版.part4.rar<br>Spring in Action. 中文版.part5.rar<br>Spring in Action. 中文版.part6.rar<br>Spring in Action. 中文版.part7.rar<br>Spring in Action. 中文版.part8.rar<br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值