认证 认可
- 认证—— Authentication 确认通信的对方是谁,通过用户名,密码,Token,验证码等各种方式
– 只是允许通过安保,进入房间后并没有任何操作房间装置的任何权限,主要有三种方式
- WHAT YOU ARE (inherence factor)
通过声音,样貌等,可以直接确认对方。 - WHAT YOU HAVE (possession factor)
通过身份证等各种证明信息,确认对方 - WHAT YOU KNOW (knowledge factor)
通过只有对方才知道的密码,Token,验证码等秘密信息,确认对方
SpringBoot的简单认证实现,可以参照Spring Security 5-认证认可
- 认可—— Authorization 授权,给通信对方一定的权限
AuthenticationManagerBuilder
WebSecurityConfigurerAdapter 中通过重写
protected void configure(AuthenticationManagerBuilder auth)
实现Inmemory,LDAP,JDBC方式的认证,同时SpringBoot提供了默认的Login界面(DefaultLoginPageConfigurer<>())
HttpSecurity
WebSecurityConfigurerAdapter 中通过重写
protected void configure(HttpSecurity http)
实现CORS ,自定义登陆 / 登出界面,是否需要认证等自定义配置机能
默认指定
http
.authorizeRequests().anyRequest().authenticated() //所有的request都需要认证
.and()
.formLogin() // 缺省login界面认证
.and()
.httpBasic(); // Basic认证
CORS
英文全称;CORS (Cross-Origin Resource Sharing)
跨原始资源共享,提供对信赖web site的跨域访问权限,允许以灵活的方式指定什么样的跨域请求被授权
Spring Security5 默认有效CORS ,实现安全的外部访问允许。
参照;Security-CORS
Spring MVC里也提供了CORS的跨域设置
参照;MVC-CORS
CSRF
Login / Logout
自定义自己的登陆界面,以及登陆成功后的跳转页面。
参照;FreeMaker
或者如果使用 Thymeleaf 2.X系列的话
步骤;
-
依赖追加
compile('org.springframework.boot:spring-boot-starter-thymeleaf') -
配置文件(application.properties)内追加
spring.thymeleaf.prefix: classpath:/templates/ -
简单html文件生成(src/main/resources/templates/test.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.min.js"></script> <script type="text/javascript"> function crosRequest(){ $.ajax({ url:'http://localhost:1234/sendmessage', type:'get', dataType:'json', success:function(data){ console.log(data); } }); } </script> </head> <body> <button onclick="crosRequest()">crosRequest</button> </body> </html> -
Controller的生成
@Controller public class sampleController { @RequestMapping("/hello") public String getHello(HashMap<String, Object> map) { map.put("hello", "Welcom Page."); return "/test"; } } -
运行测试即可
http://localhost:xxxx/hello
WebSecurity
WebSecurityConfigurerAdapter 中通过重写
protected void configure(WebSecurity http)
实现指定路径的访问控制
默认空实装
@Override
public void configure(WebSecurity http) throws Exception {
// Remove monitoring path from SpringSecurity。
http.ignoring().antMatchers("/api/**");
}

本文介绍了Spring Security的认证和授权过程,包括AuthenticationManagerBuilder的使用来配置认证,HttpSecurity用于自定义HTTP安全设置,如CORS和CSRF保护。还提到了如何自定义登录/登出界面,并通过WebSecurity配置访问控制。
7万+

被折叠的 条评论
为什么被折叠?



