javascript
Spring MVC 4快速入门Maven原型已改进
Spring Boot使Spring入門非常容易。 但是仍然有人對不使用Spring Boot并以更經典的方式引導應用程序感興趣。 幾年前,我創建了一個原型(早于Spring Boot),簡化了引導Spring Web應用程序的過程。 盡管Spring Boot已經上市了一段時間,但是Spring MVC 4 Quickstart Maven Archetype在GitHub上仍然很受歡迎。 我希望最近有了一些補充,它會更好。
Java 8
我已經決定將目標平臺切換為Java8。在生成的項目中還沒有特定的Java 8代碼,但是我相信所有新的Spring項目都應該從Java 8開始。Java 8的采用超出了預期。 看看: https : //typesafe.com/company/news/survey-of-more-than-3000-developers-reveals-java-8-adoption-ahead-of-previous-forecasts
介紹Spring IO平臺
Spring IO平臺將核心Spring API整合到了一個用于現代應用程序的緊密結合的平臺中。 。 主要優勢在于,它通過提供Spring項目的版本以及經過測試并已知可以協同工作的依賴項,簡化了依賴項管理。
以前,所有依賴項都是手動指定的,解決版本沖突需要一些時間。 使用Spring IO平臺,我們必須僅更改平臺版本(當然還要注意平臺外部的依賴性):
<dependencyManagement><dependencies><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>${io.spring.platform-version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>現在可以使用依賴項,而無需在POM中指定version :
<!-- Spring --> <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId> </dependency> <!-- Security --> <dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId> </dependency> <dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId> </dependency>Java安全性配置
當我第一次創建原型時,無法使用Java代碼配置Spring Security。 但是現在是了,所以我將XML配置遷移到Java配置。
SecurityConfig現在從WebSecurityConfigurerAdapter擴展,并用@Configuration和@EnableWebMvcSecurity批注標記。
安全配置詳細信息
限制訪問除以下以外的每個URL
XML配置:
<security:intercept-url pattern="/" access="permitAll" /> <security:intercept-url pattern="/resources/**" access="permitAll" /> <security:intercept-url pattern="/signup" access="permitAll" /> <security:intercept-url pattern="/**" access="isAuthenticated()" />成為:
http.authorizeRequests().antMatchers("/", "/resources/**", "/signup").permitAll().anyRequest().authenticated()登錄/注銷
XML配置:
<security:form-login login-page="/signin" authentication-failure-url="/signin?error=1"/> <security:logout logout-url="/logout" />成為:
http.formLogin().loginPage("/signin").permitAll().failureUrl("/signin?error=1").loginProcessingUrl("/authenticate").and().logout().logoutUrl("/logout").permitAll().logoutSuccessUrl("/signin?logout");記住賬號
XML配置:
<security:remember-me services-ref="rememberMeServices" key="remember-me-key"/>成為:
http.rememberMe().rememberMeServices(rememberMeServices()).key("remember-me-key");CSRF已啟用用于生產而已禁用以進行測試
當前默認情況下啟用CSRF,因此不需要其他配置。 但是在進行集成測試時,我想確保CSRF被禁用。 我找不到執行此操作的好方法。 我從傳遞給CsrfConfigurer CSRF保護匹配器開始,但是最后得到了很多我不希望在SecurityConfiguration擁有的代碼。 我最終得到了一個NoCsrfSecurityConfig ,它從原始SecurityConfig擴展并禁用了CSRF:
@Configuration public class NoCsrfSecurityConfig extends SecurityConfig {@Overrideprotected void configure(HttpSecurity http) throws Exception {super.configure(http);http.csrf().disable();} }連接池
HikariCP現在用作生成的應用程序中的默認連接池。 使用默認配置:
@Bean public DataSource configureDataSource() {HikariConfig config = new HikariConfig();config.setDriverClassName(driver);config.setJdbcUrl(url);config.setUsername(username);config.setPassword(password);config.addDataSourceProperty("cachePrepStmts", "true");config.addDataSourceProperty("prepStmtCacheSize", "250");config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");config.addDataSourceProperty("useServerPrepStmts", "true");return new HikariDataSource(config); }還有更多
Spring MVC 4快速入門Maven原型還遠遠沒有完成。 由于Spring平臺涉及原型,因此必須進行相應調整。 我很期待聽到可以做些什么來使其成為一個更好的項目。 如果有想法或建議,請在GitHub上發表評論或創建問題。
參考文獻
- Spring MVC 4快速入門Maven原型
翻譯自: https://www.javacodegeeks.com/2014/12/spring-mvc-4-quickstart-maven-archetype-improved.html
總結
以上是生活随笔為你收集整理的Spring MVC 4快速入门Maven原型已改进的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: aigo爱国者GTV2智能手表上市 突破
- 下一篇: 上汽8月整车销量42.3万辆 新能源销量