Skip to content

Commit

Permalink
2018/06/23 15:40:33 ____ :sun_with_face::sunflower::palm_tree::house_…
Browse files Browse the repository at this point in the history
…with_garden::office::octocat::guitar::meat_on_bone:
  • Loading branch information
timebusker committed Jun 23, 2018
1 parent 5d299df commit cbeefd7
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 223 deletions.
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -72,4 +72,15 @@ Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务。你可

#### 第十九个模块:[......................................Spring Boot自定义Starter](https://github.com/timebusker/spring-boot/tree/master/spring-boot-19-Definition-Starter/)

----
----

### 关于Spring Boot模板引擎
- 虽然现在很多开发,都采用了前后端完全分离的模式,即后端只提供数据接口,前端通过AJAX请求获取数据,完全不需要用的模板引擎。
这种方式的优点在于前后端完全分离,并且随着近几年前端工程化工具和MVC框架的完善,使得这种模式的维护成本相对来说也更加低一点。
但是这种模式不利于SEO,并且在性能上也会稍微差一点,还有一些场景,使用模板引擎会更方便,比如说邮件模板。

- 总体来讲,Spring boot对thymeleaf和Freemaker支持比较友好,配置相对也简单一点,Spring Boot不建议使用JSP,因为在使用嵌入式servlet容器时,有一些使用限制。
在实际的开发中,大多也以这两种模板引擎为主,很少有用jsp的,jsp现在可能更多是在实验或者学习阶段使用。

![image](https://github.com/timebusker/spring-boot/raw/master/static/111111.png?raw=true)
[性能对比测试](https://github.com/jreijn/spring-comparing-template-engines)
3 changes: 3 additions & 0 deletions _git_pull.cmd
@@ -0,0 +1,3 @@
color 0a
@echo off & setlocal
git pull
12 changes: 12 additions & 0 deletions _git_push.cmd
@@ -0,0 +1,12 @@
color 0a
@echo off & setlocal
:: cd img/top-photo/
:: call 0-rename-jpg.bat
:: cd ..

set var=":sun_with_face::sunflower::palm_tree::house_with_garden::office::octocat::guitar::meat_on_bone:"
set d=%date:~0,10%
set t=%time:~0,8%
git add .
git commit -am"%d% %t% ____ %var%"
git push origin master
45 changes: 0 additions & 45 deletions _gitbash.cmd

This file was deleted.

@@ -1,92 +1,92 @@
package cn.timebusker.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
* @DESC:SpringSecurityConfig
* @author: timebusker
* @date:2018/5/3
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启@PreAuthorize注解
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UrlsDao urlsDao;
@Autowired
private UserInfoDao userInfoDao;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsServiceBean());
//密码MD5加密配置
auth.authenticationProvider(authenticationProvider());
}

@Bean
public DaoAuthenticationProvider authenticationProvider() throws Exception {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsServiceBean());
authenticationProvider.setPasswordEncoder(new Md5PasswordEncoder());
return authenticationProvider;
}

@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
return new CustomUserService(userInfoDao, urlsDao);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
//.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/js/**", "/css/**", "/images/**").permitAll()
.antMatchers("/logout").permitAll()
.antMatchers("/user/list").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", false)
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/nolimit")
.and()
.httpBasic();
}

/**
* 加载自定义过滤器
*
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(someFilter());
registration.addUrlPatterns("/*");
// registration.addInitParameter("paramName", "paramValue");
registration.setName("commonFilter");
registration.setOrder(1);
return registration;
}

@Bean(name = "commonFilter")
public Filter someFilter() {
return new CommonFilter();
}


}
//package cn.timebusker.security;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
//import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.core.userdetails.UserDetailsService;
//
///**
// * @DESC:SpringSecurityConfig
// * @author: timebusker
// * @date:2018/5/3
// */
//@EnableWebSecurity
//@EnableGlobalMethodSecurity(prePostEnabled = true)//开启@PreAuthorize注解
//public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
//
// @Autowired
// private UrlsDao urlsDao;
// @Autowired
// private UserInfoDao userInfoDao;
//
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.userDetailsService(userDetailsServiceBean());
// //密码MD5加密配置
// auth.authenticationProvider(authenticationProvider());
// }
//
// @Bean
// public DaoAuthenticationProvider authenticationProvider() throws Exception {
// DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
// authenticationProvider.setUserDetailsService(userDetailsServiceBean());
// authenticationProvider.setPasswordEncoder(new Md5PasswordEncoder());
// return authenticationProvider;
// }
//
// @Override
// public UserDetailsService userDetailsServiceBean() throws Exception {
// return new CustomUserService(userInfoDao, urlsDao);
// }
//
// @Override
// protected void configure(HttpSecurity http) throws Exception {
// http
// //.csrf().disable()
// .authorizeRequests()
// .antMatchers("/").permitAll()
// .antMatchers("/js/**", "/css/**", "/images/**").permitAll()
// .antMatchers("/logout").permitAll()
// .antMatchers("/user/list").permitAll()
// .anyRequest().authenticated()
// .and()
// .formLogin()
// .loginPage("/login")
// .defaultSuccessUrl("/", false)
// .permitAll()
// .and()
// .exceptionHandling().accessDeniedPage("/nolimit")
// .and()
// .httpBasic();
// }
//
// /**
// * 加载自定义过滤器
// *
// * @return
// */
// @Bean
// public FilterRegistrationBean someFilterRegistration() {
// FilterRegistrationBean registration = new FilterRegistrationBean();
// registration.setFilter(someFilter());
// registration.addUrlPatterns("/*");
//// registration.addInitParameter("paramName", "paramValue");
// registration.setName("commonFilter");
// registration.setOrder(1);
// return registration;
// }
//
// @Bean(name = "commonFilter")
// public Filter someFilter() {
// return new CommonFilter();
// }
//
//
//}
67 changes: 31 additions & 36 deletions spring-boot-6-GlobalException/src/main/java/cn/timebusker/App.java
Expand Up @@ -9,40 +9,35 @@
@SpringBootApplication
public class App {

/**
* @SpringBootApplication注解的类一定要放在自定义包下且属于自定义包的
*
* @SpringBootApplication is a convenience annotation that adds all of the following:
* @SpringBootApplication是一个方便的注解,增加了所有的以下内容:
*
* @Configuration tags the class as a source of bean definitions for the application context.
* @Configuration 标记一个类来作为bean定义的应用程序上下文的资源
*
* @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
* @EnableAutoConfiguration告诉Spring Boot开始加载基于类路径下的配置信息、beans、各种属性配置。
*
* Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds
* it automatically when it sees spring-webmvc on the classpath. This flags the application as a web
* application and activates key behaviors such as setting up a DispatcherServlet
* 通常你会添加@EnableWebMvc为Spring MVC的应用程序,但是当Spring Boot在classpath下检索到spring-webmvc时,
* spring boot会自动添加。这标志该应用程序作为Web应用程序,并激活关键行为,如设立的DispatcherServlet
*
* @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
* @ComponentScan 告诉Spring寻找其他组件,配置,以及业务层类,最前面才能加载到所有的类。
*/

public static void main(String[] args) {
// devtools:是spring boot的一个热部署工具
//设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性.
//System.setProperty("spring.devtools.restart.enabled","false");

// 启动Sprign Boot
ApplicationContext ctx = SpringApplication.run(App.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
/**
* @SpringBootApplication注解的类一定要放在自定义包下且属于自定义包的
* @SpringBootApplication is a convenience annotation that adds all of the following:
* @SpringBootApplication是一个方便的注解,增加了所有的以下内容:
* @Configuration tags the class as a source of bean definitions for the application context.
* @Configuration 标记一个类来作为bean定义的应用程序上下文的资源
* @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
* @EnableAutoConfiguration告诉Spring Boot开始加载基于类路径下的配置信息、beans、各种属性配置。
* <p>
* Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds
* it automatically when it sees spring-webmvc on the classpath. This flags the application as a web
* application and activates key behaviors such as setting up a DispatcherServlet
* 通常你会添加@EnableWebMvc为Spring MVC的应用程序,但是当Spring Boot在classpath下检索到spring-webmvc时,
* spring boot会自动添加。这标志该应用程序作为Web应用程序,并激活关键行为,如设立的DispatcherServlet
* @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
* @ComponentScan 告诉Spring寻找其他组件,配置,以及业务层类,最前面才能加载到所有的类。
*/

public static void main(String[] args) {
// devtools:是spring boot的一个热部署工具
//设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性.
//System.setProperty("spring.devtools.restart.enabled","false");
// 启动Sprign Boot
ApplicationContext ctx = SpringApplication.run(App.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
// String[] beanNames = ctx.getBeanDefinitionNames();
// Arrays.sort(beanNames);
// for (String beanName : beanNames) {
// System.out.println(beanName);
// }
}
}

0 comments on commit cbeefd7

Please sign in to comment.