|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "spring boot项目练习(一)整合spring security登录" |
| 4 | +date: 2020-12-26 17:26:35 +0800 |
| 5 | +tags: java |
| 6 | +description: |
| 7 | +--- |
| 8 | + |
| 9 | +最近看了好多spring boot的资料,也一直在学习,准备动手,自己写一个接口测试的系统来使用,正好也有这个需求。 |
| 10 | + |
| 11 | +自己会尽量的把开发过程中遇到的问题,或者一些东西记录下来,尽量不太监。。哈哈 |
| 12 | + |
| 13 | +对于用户登录,本来想使用之前用过的shiro,但是spring boot已经默认配置好了spring security。索性就直接使用security来管理用户 |
| 14 | + |
| 15 | +## 引入包 |
| 16 | + |
| 17 | +> 只需要简单的引入两个包就可以使用了 |
| 18 | +
|
| 19 | +{% highlight java %} |
| 20 | +<dependency> |
| 21 | + <groupId>org.springframework.boot</groupId> |
| 22 | + <artifactId>spring-boot-starter-security</artifactId> |
| 23 | +</dependency> |
| 24 | +<dependency> |
| 25 | + <groupId>org.springframework.security</groupId> |
| 26 | + <artifactId>spring-security-test</artifactId> |
| 27 | + <scope>test</scope> |
| 28 | +</dependency> |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +## 配置登录 |
| 32 | + |
| 33 | +> 对于security默认的登录,不在介绍了,引入包之后,再次访问会进入默认的登录页面,默认的登录名是user,密码会打印在控制台中 |
| 34 | +> 默认形式,基本不会使用在项目中,我们这里直接介绍自定义登录和响应的用法 |
| 35 | +
|
| 36 | +### 控制器+页面 |
| 37 | + |
| 38 | +> 控制器和页面几乎不需要多余的操作 |
| 39 | +> 控制器只需要渲染视图 |
| 40 | +> 页面只需要进行提交就可以了 |
| 41 | +> 其他的认证操作都交给security去处理就可以了 |
| 42 | +
|
| 43 | +控制器 |
| 44 | + |
| 45 | +{% highlight java %} |
| 46 | +@Controller(value = "loginController") |
| 47 | +@RequestMapping(value = "login") |
| 48 | +public class LoginController extends BaseController { |
| 49 | + |
| 50 | + @RequestMapping(value = "index") |
| 51 | + public ModelAndView index(){ |
| 52 | + return result("login/index"); |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | +{% endhighlight %} |
| 57 | + |
| 58 | +视图,这里是贴上了form表单和提交js事件,资源文件的引入之类的没有写入,防止无用的东西占用篇幅 |
| 59 | + |
| 60 | +{% highlight html %} |
| 61 | +<!DOCTYPE html> |
| 62 | +<html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| 63 | +<head> |
| 64 | + <title>SB Admin 2 - Login</title> |
| 65 | +</head> |
| 66 | +<body class="bg-gradient-primary"> |
| 67 | +<form class="user"> |
| 68 | + <div class="form-group"> |
| 69 | + <input type="text" class="form-control form-control-user" name="username" placeholder="用户名"> |
| 70 | + </div> |
| 71 | + <div class="form-group"> |
| 72 | + <input type="password" class="form-control form-control-user" name="password" placeholder="密码"> |
| 73 | + </div> |
| 74 | + <div class="form-group"> |
| 75 | + <div class="custom-control custom-checkbox small"> |
| 76 | + <input type="checkbox" class="custom-control-input" id="customCheck"> |
| 77 | + <label class="custom-control-label" for="customCheck">Remember Me</label> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + <button type="button" id="submit" class="btn btn-primary btn-user btn-block"> |
| 81 | + Login |
| 82 | + </button> |
| 83 | +</form> |
| 84 | +</body> |
| 85 | +<script> |
| 86 | + $(function () { |
| 87 | + $("#submit").click(function () { |
| 88 | + submitForm("/login/index",function (message) { |
| 89 | + showSuccess(message); |
| 90 | + },function (message) { |
| 91 | + showWarning(message); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }) |
| 95 | +</script> |
| 96 | +</html> |
| 97 | +{% endhighlight %} |
| 98 | + |
| 99 | +### security自定义用户认证配置 |
| 100 | + |
| 101 | +> 需要继承UserDetailsService接口类,并实现其中的函数 |
| 102 | +> 对于User对象,权限字符串不可直接给null值 |
| 103 | +> 在UserService中,用户名验证成功后,user对象会在下一步,进行密码验证 |
| 104 | +
|
| 105 | +{% highlight java %} |
| 106 | +@Component |
| 107 | +public class UserService implements UserDetailsService { |
| 108 | + |
| 109 | + @Autowired |
| 110 | + private AdminMapper adminMapper; |
| 111 | + |
| 112 | + @Override |
| 113 | + public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { |
| 114 | + //根据用户名查询信息 |
| 115 | + QueryWrapper<AdminEntity> sql = new QueryWrapper<>(); |
| 116 | + sql.eq("username",s); |
| 117 | + AdminEntity adminEntity = adminMapper.selectOne(sql); |
| 118 | + if(adminEntity == null){ |
| 119 | + throw new UsernameNotFoundException("用户名或密码错误!"); |
| 120 | + } |
| 121 | + //用户验证通过,构建User对象,进行密码验证 |
| 122 | + //用户权限不能为空,先生成默认权限 |
| 123 | + List authz = AuthorityUtils.commaSeparatedStringToAuthorityList("admin"); |
| 124 | + User user = new User(adminEntity.getUsername(), adminEntity.getPassword(), authz); |
| 125 | + |
| 126 | + return user; |
| 127 | + } |
| 128 | +} |
| 129 | +{% endhighlight %} |
| 130 | + |
| 131 | +### 自定义MD5密码验证 |
| 132 | + |
| 133 | +> 自定义密码验证类,需要继承PasswordEncoder接口类 |
| 134 | +> 继承后,实现encode和matches函数,前者是加密,后者是匹配 |
| 135 | +> 代码中得MD5是自定义的工具类 |
| 136 | +
|
| 137 | +{% highlight java %} |
| 138 | +public class MD5Password implements PasswordEncoder { |
| 139 | + |
| 140 | + @Override |
| 141 | + public String encode(CharSequence charSequence) { |
| 142 | + return MD5.encode(charSequence.toString()); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean matches(CharSequence charSequence, String s) { |
| 147 | + return MD5.valid(charSequence.toString(),s); |
| 148 | + } |
| 149 | +} |
| 150 | +{% endhighlight java %} |
| 151 | + |
| 152 | +### 自定义相应 |
| 153 | + |
| 154 | +> 大部分业务逻辑中,对于成功和失败的响应都需要有固定的格式,security当然也提供了这种功能 |
| 155 | +> 成功响应继承AuthenticationSuccessHandler接口类 |
| 156 | +> 失败响应继承AuthenticationFailureHandler接口类 |
| 157 | +
|
| 158 | +{% highlight java %} |
| 159 | +//成功 |
| 160 | +public class SuccessHandler implements AuthenticationSuccessHandler { |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { |
| 164 | + R r = R.ok().message("登录成功!"); |
| 165 | + //转为jsonobject对象 |
| 166 | + JsonObject jsonObject = JsonUtil.toJsonObject(r); |
| 167 | + //设置返回字符串和返回类型 |
| 168 | + httpServletResponse.setCharacterEncoding("utf-8"); |
| 169 | + httpServletResponse.setContentType("application/json;charset=utf-8"); |
| 170 | + //打印字符串 |
| 171 | + PrintWriter out = null; |
| 172 | + out = httpServletResponse.getWriter(); |
| 173 | + out.write(jsonObject.toString()); |
| 174 | + out.close(); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +//失败 |
| 179 | +public class FailureHandler implements AuthenticationFailureHandler { |
| 180 | + |
| 181 | + @Override |
| 182 | + public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException { |
| 183 | + R r = R.error().code(200).message(用户名或密码错误!"); |
| 184 | + //转为jsonobject对象 |
| 185 | + JsonObject jsonObject = JsonUtil.toJsonObject(r); |
| 186 | + //设置返回字符串和返回类型 |
| 187 | + httpServletResponse.setCharacterEncoding("utf-8"); |
| 188 | + httpServletResponse.setContentType("application/json;charset=utf-8"); |
| 189 | + //打印字符串 |
| 190 | + PrintWriter out = null; |
| 191 | + out = httpServletResponse.getWriter(); |
| 192 | + out.write(jsonObject.toString()); |
| 193 | + out.close(); |
| 194 | + } |
| 195 | +} |
| 196 | +{% endhighlight %} |
| 197 | + |
| 198 | +### 整合上述配置 |
| 199 | + |
| 200 | +{% highlight java %} |
| 201 | +@Configuration |
| 202 | +@EnableWebSecurity |
| 203 | +public class CustomerConfiguration extends WebSecurityConfigurerAdapter { |
| 204 | + |
| 205 | + @Override |
| 206 | + protected void configure(HttpSecurity http) throws Exception { |
| 207 | + http.formLogin() |
| 208 | + //登录页面 |
| 209 | + .loginPage("/login/index") |
| 210 | + //登录请求url,同页面中ajax请求url |
| 211 | + .loginProcessingUrl("/login/index") |
| 212 | + //设置自定义成功返回 |
| 213 | + .successHandler(new SuccessHandler()) |
| 214 | + //设置自定义失败返回 |
| 215 | + .failureHandler(new FailureHandler()) |
| 216 | + .permitAll(); |
| 217 | + |
| 218 | + http.authorizeRequests() |
| 219 | + //不需要认证就能访问的url |
| 220 | + .antMatchers("/login/**","/static/**") |
| 221 | + .permitAll() |
| 222 | + //其余url都需要经过认证 |
| 223 | + .anyRequest().authenticated(); |
| 224 | + |
| 225 | + //禁用csrf保护 |
| 226 | + http.csrf().disable(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * 配置自定义加密类 |
| 231 | + * @return |
| 232 | + */ |
| 233 | + @Bean |
| 234 | + public PasswordEncoder passwordEncoder(){ |
| 235 | + return new MD5Password(); |
| 236 | + } |
| 237 | +} |
| 238 | +{% endhighlight %} |
| 239 | + |
| 240 | +配置完成后的使用效果 |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +两次请求的结果都是由security中的自定义配置中返回 |
0 commit comments