Spring boot 使用restful风格的spring security login
2016-10-17 11:53
639 查看
第一步,获取基本springboot项目
从 spring官网获取security+session+redis依赖的springboot项目安装配置redis,修改配置文件,启动项目,拿到登录密码,用户名为user.
根据官方文档,配置demo项目,理解spring security的机制
第二步,添加自己的登录验证方式
添加session策略,让权限验证从session里获取token,而不是默认的cookie重写自己的UserDetails,实现org.springframework.security.core.userdetails.UserDetails接口
重写自己的UserService,实现org.springframework.security.core.userdetails.UserDetailsService
接口,重写loadUserByUsername方法,从数据库根据username获取UserDetails
在WebSecuirtyConfig里加入配置:
/** * Configure order web security with authentication manager build */ @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(hanoverUserService).passwordEncoder(bcryptEncoder); }
第三步,重写过滤器,修改form方式为标准的post
spring security的用户密码获取在org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
的attemptAuthentication方法里,我们需要新建一个过滤器,继承UsernamePasswordAuthenticationFilter过滤器,并且重写attemptAuthentication方法。
直接从body里获取json参数:
ObjectMapper mapper = new ObjectMapper(); Map result = null; result = mapper.readValue(request.getReader(), Map.class); String username = (String)result.get("username"); String password = (String)result.get("password");
然后再WebSecurityConfig里添加该filter:
http.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
这样的话,就会把之前的form方式变为现在的json传参。
第四步,添加自己的successHandle和failureHandle,用标准restful返回
获取参数的方式修改了,验证的方式也修改了,现在就来修改返回结果的方式新建RESTAuthenticationSuccessHandler继承SimpleUrlAuthenticationSuccessHandler
重写onAuthenticationSuccess方法,把标准的CommonData返回结果写入response,代码如下:
CommonData<Map> result = new CommonData<Map>(); Map<String, Object> tokenMap = new HashMap<String, Object>(); response.setStatus(HttpStatus.OK.value()); response.setContentType(MediaType.APPLICATION_JSON_VALUE); tokenMap.put("token", request.getSession().getId()); result.setData(tokenMap); mapper.writeValue(response.getWriter(), result);
同样,新建RESTAuthenticationFailureHandler继承SimpleUrlAuthenticationFailureHandler
重写onAuthenticationFailure方法,把标准的commonData返回结果写入response:
BastDTOData result = new BastDTOData(); result.setSuccess(false); response.setStatus(HttpStatus.UNAUTHORIZED.value()); response.setContentType(MediaType.APPLICATION_JSON_VALUE); mapper.writeValue(response.getWriter(), result);
第五步,解决跨域问题
解决完如上问题之后,在本地和postman上都可以正常用restful风格的访问方式请求登录,但是使用前端代码进行跨域访问的时候,会有跨域问题在successHandle和failureHandle里,需要在header里进行如下操作:
final String origin = request.getHeader("origin"); if (StringUtils.isNotEmpty(origin)) { response.setHeader("Access-Control-Allow-Origin", origin); } else { response.setHeader("Access-Control-Allow-Origin", "*"); } response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "OPTIONS,GET,PUT,POST,DELETE,HEAD"); response.setHeader("Access-Control-Allow-Headers", "content-type,access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with,serviceName,locale");
其他资料
RESTful authentication using Spring Security on Spring Boot, and jQuery as a web client 链接Spring Boot Security Application 链接
REST Authentication using Spring Security & Spring Session链接
Understanding CORS链接
相关文章推荐
- RESTful风格(使用Ajax+Spring MVC框架实现)
- springBoot+springSecurity 动态管理Restful风格权限(三)
- Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations
- 使用cxf,spring,mybatis框架完成restful风格的webservice服务
- 使用SpringBoot编写Restful风格接口
- 在spring中使用webservice(Restful风格)
- 在Spring中使用Springfox和swagger生成restful风格的API文档
- RESTful风格(使用Ajax+Spring MVC框架实现)
- 使用 SpringBoot 写 RESTful风格 增删改查接口
- 使用spring boot 整合spring jpa做基于restful风格的增删改查(maven ,mysql)
- 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用
- springBoot+springSecurity 动态管理Restful风格权限(三)
- jersey+spring+mybatis,使用jetty容器,druid连接池,构建restful风格项目
- spring boot之使用springfox swagger展示restful的api doc
- 使用 Spring 3 来创建 RESTful Web Services
- spring和resteasy集成,构建restful风格服务
- 使用Jeety与Restlet构建Restful风格WebService
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 使用spring-security3.1 + spring mvc + Hibernate 控制系统权限