springboot-springSecurity 之 http Basic认证 (四)
来源:互联网 发布:如何快速学完网络课程 编辑:程序博客网 时间:2024/06/05 05:40
引言:
HTTP基础认证(BA)是一种简单的认证机制。当一个web客户端需要保护任何web资源的时候,服务器会发送一个带有401状态码(未授权)的HTTP回应,还有类似WWW-Authenticate: Basic realm=”realm here” 的 WWW-Authenticate HTTP头。而浏览器这时候就会弹出一个登录对话框,提示输入用户名和密码。
1. 修改配置
在spring boot项目中实现Spring Security进行http Basic认证非常简单,只需在配置文件中增加 .httpBasic(); 直接配置即可使用
@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UrlUserService urlUserService; @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers(DATA_COLLECT_RAW_URL).permitAll() .antMatchers("/login").permitAll() .antMatchers("/logout").permitAll() .antMatchers("/images/**").permitAll() .antMatchers("/js/**").permitAll() .antMatchers("/css/**").permitAll() .antMatchers("/fonts/**").permitAll() .antMatchers("/").permitAll() .anyRequest().authenticated() .and() .sessionManagement() .and() .logout() .and() .httpBasic(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(urlUserService).passwordEncoder(new PasswordEncoder() {//此处为密码使用md5 进行加密 @Override public String encode(CharSequence rawPassword) { return MD5Util.encode((String) rawPassword); } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(MD5Util.encode((String) rawPassword)); } }); }}
2. 登录方式的变化
http Basic 实际上就是将我门的用户名和密码连接起来然后 使用base64进行加密,将加密后的密文放在http 的header 中进行验证。
帐号密码加密如下 (假设账号密码都为admin)admin:admin base64 加密后为 YWRtaW46YWRtaW4=加密后的串放入 header 时应在拼结为 Basic YWRtaW46YWRtaW4= 注意:Basic 与密码串之间为一个空格
postMan 请求如下:
3.在controller 中获取请求参数
由于登录是security 进行验证的,验证成功后会跳转到 “/login“ api,所以我门要定义自己login api
@AuthenticationPrincipal 注解是为了从security 中获取登录后的user 信息。
登录成功后返回用户信息。
当登出后也会进入”/login” api ,登出成功返回null
@RequestMapping(value = "/login") @ResponseBody //用户名密码是用base64 加密 原文为 admin:admin 即 用户名:密码 内容是放在request.getHeader 的 "authorization" 中 public Object login(@AuthenticationPrincipal User loginedUser, @RequestParam(name = "logout", required = false) String logout) { if (logout != null) { return null; } if (loginedUser != null) { return loginedUser; } return null; }
关于security 的Http Basic 验证到此就告一段落了。敬请期待。
2 0
- springboot-springSecurity 之 http Basic认证 (四)
- springboot-springSecurity 之 http Basic认证 (四)
- Http认证之Basic认证
- SpringBoot-SpringSecurity集成-修定
- HTTP Basic Authentication认证
- Http Basic Digest 认证
- HTTP basic 认证
- HTTP Basic Authentication认证
- HTTP Basic Authentication认证
- HTTP Basic 认证(Authentication)
- HTTP Basic Authentication认证
- 在SpringBoot项目中使用SpringSecurity权限认证框架
- HTTP认证教程(四)
- HTTP学习(四) 认证
- HTTP基本认证(HTTP Basic Athorization)过程分析
- HTTP基本认证(HTTP Basic Athorization)过程分析
- HTTP基本认证(HTTP Basic Athorization)过程分析
- HTTP基本认证(HTTP Basic Athorization)过程分析
- MySQL数据库liunx 下的自动备份
- eclipse 代码格式自定义
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 如何锻造出让用户欲罢不能的鸦片式文章?
- bootstrap-table表格里的操作栏,点击删除图标给出删除提示
- springboot-springSecurity 之 http Basic认证 (四)
- Win32下定时器与多线程
- Php web测试工具simpletest使用
- 欢迎使用CSDN-markdown编辑器
- JAVA从入门到精通(5)——数组
- pat 最大子列和问题
- 快速排序算法
- HTML嵌入到iOS页面的开发
- codeforces 776C Molly's Chemicals(前缀和 二分搜索)