使用Spring boot+Spring security+thymeleaf实现简单的基于内存的表单登录功能,之后陆续添加基于数据库的登录、密码加密、权限分配、使用spring security oauth2第三方登录,并集成websocket实现一个简单的聊天室。
工具:eclipse、maven。
在eclipse上创建Maven项目,在pom.xml的<project>标签下添加:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
在<dependencies>标签下添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
保存,更新项目,右击项目名->Maven->Update Project。
程序入口——App.java:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
创建一个config包,在包下创建一个java类,继承WebSecurityConfigurerAdapter类,为了实现基于内存的登录,重写configure方法:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
}
}
第一个方法是在内存中添加一个用户名和密码均为“admin”的账号,具有“ADMIN”的角