|
SpringBoot1X只需要spring-boot-starter-thymeleaf的依赖 项 来启用Web应用程序中的 Thymeleaf支持。但是由于Thymeleaf3.0中的新功能, 我们必须将thymeleaf-layout-dialect 添加 为SpringBoot2XWeb应用程序中的依赖项。一旦依赖关系到位,我们就可以将模板添加到src/main/resources/templates文件夹中,SpringBoot将自动显示它们。
4.4、Spring Security 配置
为简单起见,我们使用框架默认的HTTP Basic身份验证。让我们首先看一下使用Spring启用Security所需的依赖关系和配置。
Spring首先需要依赖 spring-security-web和spring-security-config 模块。接下来, 我们需要添加一个扩展WebSecurityConfigurerAdapter的类,并使用@EnableWebSecurity注解:
- @Configuration
- @EnableWebSecurity
- public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
-
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
- auth.inMemoryAuthentication()
- .withUser("admin")
- .password(passwordEncoder()
- .encode("password"))
- .authorities("ROLE_ADMIN");
- }
-
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests()
- .anyRequest().authenticated()
- .and()
- .httpBasic();
- }
-
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- }
这里我们使用inMemoryAuthentication来设置身份验证。同样,Spring Boot也需要这些依赖项才能使其工作。但是我们只需要定义spring-boot-starter-security的依赖关系,因为这会自动将所有相关的依赖项添加到类路径中。
Spring Boot中的安全配置与上面的相同。
5、应用程序引导配置
Spring和Spring Boot中应用程序引导的基本区别在于servlet。
Spring使用web.xml 或SpringServletContainerInitializer作为其引导入口点。
Spring Boot仅使用Servlet 3功能来引导应用程序,,下面让我们详细来了解下
5.1、Spring 是怎样引导配置的呢?
Spring支持传统的web.xml引导方式以及最新的Servlet 3+方法。
让我们看一下 web.xml方法的步骤:
Servlet容器(服务器)读取web.xml
web.xml中定义的DispatcherServlet由容器实例化
DispatcherServlet通过读取WEB-INF / {servletName} -servlet.xml来创建WebApplicationContext
最后,DispatcherServlet注册在应用程序上下文中定义的bean
以下是使用Servlet 3+方法的Spring引导:
容器搜索实现ServletContainerInitializer的类并执行
SpringServletContainerInitializer找到实现所有类WebApplicationInitializer
WebApplicationInitializer创建具有XML或上下文@Configuration类
WebApplicationInitializer创建DispatcherServlet的 与先前创建的上下文。
5.2、SpringBoot 有是如何配置的呢?
Spring Boot应用程序的入口点是使用@SpringBootApplication注释的类:
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
(编辑:应用网_阳江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|