boot+security+thymeleaf+Bootstarp实例

  • 项目简介
本项目在Spring boot的基础上继承Spring security功能。注:作为自己的学习心得记录下来,欢迎交流,不喜勿碰。
  • 项目目录
boot+security+thymeleaf+Bootstarp实例_第1张图片
主要的文件如上红框所示。
  • 项目代码

pom.xml



	4.0.0

	huahua
	login02
	0.0.1-SNAPSHOT
	jar

	login02
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.9.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-security
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.security.web
			spring-security-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.security
			spring-security-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


WebConfig.java

package huahua.login02.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author: zhanghuadi
 * @date: 2018/1/29 0029
 * @description: 用于注入静态资源
 */
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
        //registry.addResourceHandler("ml/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"ml/");
        super.addResourceHandlers(registry);
    }
}

WebSecurityConfig.java

package huahua.login02.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author: zhanghuadi
 * @date: 2018/1/29 0029
 * @description: 用于配置安全策略
 */

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启security注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    /**
     * 设定登陆的账号和密码
     * @param auth
     * @throws Exception
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

    /**
     * 配置安全策略,即那些请求需要权限控制
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/","/index").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/hello")
                    .permitAll()
                    .and()
                .logout()
                    .logoutSuccessUrl("/index")
                    .permitAll();
    }


    /**
     * 此方法忽略对静态志愿的拦截,这样才能正常加载静态志愿文件
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/css/*.*");
        //可以仿照上面一句忽略静态资源
    }
}

PageController.java

package huahua.login02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author: zhanghuadi
 * @date: 2018/1/29 0029
 * @description: 用于返回请求页面
 */
@Controller
public class PageController {

    @RequestMapping(value = "/")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/index")
    public String index1(){
        return "index";
    }

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }
}

index.html




    
    index


    

index

Click here to see hello.

hello.html




    
    hello
    


    

hello!你好!你现在访问页面是登陆过后才能看见的页面。

login.html




    
    login
    
    
    
    
    
    
    
    



 sign.css
body {
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #eee;
}

.form-signin {
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
  margin-bottom: 10px;
}
.form-signin .checkbox {
  font-weight: normal;
}
.form-signin .form-control {
  position: relative;
  height: auto;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
此css文件才是最重要的,项目中的其他css文件是用于测试的,没有必要使用。

  • 项目总结

 本项目参考security官方教程,在原Spring boot项目上引入了Bootstrap,在项目过程中不能正常引入css文件,最终使用WebSecurityConfig.java中的
web.ignoring().antMatchers("/static/css/*.*");

 方能正常使用。

你可能感兴趣的:(Spring,Boot)