java SpringSecurity框架

  • z12345678910
    了解作者
  • Java
    开发工具
  • 30.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 6 积分
    下载积分
  • 0
    下载次数
  • 2022-05-26 18:28
    上传日期
java SpringSecurity框架
SpringSecurityDemo.zip
内容介绍
package com.robod.filter; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import com.robod.config.RsaKeyProperties; import com.robod.entity.SysRole; import com.robod.entity.SysUser; import com.robod.utils.JwtUtils; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author Robod * @date 2020/8/10 7:54 * 认证过滤器 */ public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter { private AuthenticationManager authenticationManager; private RsaKeyProperties rsaKeyProperties; public JwtLoginFilter(AuthenticationManager authenticationManager, RsaKeyProperties rsaKeyProperties) { this.authenticationManager = authenticationManager; this.rsaKeyProperties = rsaKeyProperties; } /** * 这个方法是用来去尝试验证用户的,父类中是从POST请求的form表单中获取,但是这里不是,所以需要重写 * @param request * @param response * @return * @throws AuthenticationException */ @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { try { SysUser user = JSONObject.parseObject(request.getInputStream(),SysUser.class); return authenticationManager.authenticate( new UsernamePasswordAuthenticationToken( user.getUsername(), user.getPassword()) ); } catch (Exception e) { try { response.setContentType("application/json;charset=utf-8"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); PrintWriter out = response.getWriter(); Map<String, Object> map = new HashMap<>(); map.put("code", HttpServletResponse.SC_UNAUTHORIZED); map.put("message", "账号或密码错误!"); out.write(new ObjectMapper().writeValueAsString(map)); out.flush(); out.close(); } catch (Exception e1) { e1.printStackTrace(); } throw new RuntimeException(e); } } /** * 成功之后执行的方法,父类中是放入session,不符合我们的要求,所以重写 * @param request * @param response * @param chain * @param authResult * @throws IOException * @throws ServletException */ @Override public void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { SysUser sysUser = new SysUser(); sysUser.setUsername(authResult.getName()); sysUser.setRoles((List<SysRole>) authResult.getAuthorities()); String token = JwtUtils.generateTokenExpireInMinutes(sysUser,rsaKeyProperties.getPrivateKey(),24*60); response.addHeader("Authorization", "RobodToken " + token); try { //登录成功时,返回json格式进行提示 response.setContentType("application/json;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); PrintWriter out = response.getWriter(); Map<String, Object> map = new HashMap<String, Object>(4); map.put("code", HttpServletResponse.SC_OK); map.put("message", "登陆成功!"); out.write(new ObjectMapper().writeValueAsString(map)); out.flush(); out.close(); } catch (Exception e1) { e1.printStackTrace(); } } }
评论
    相关推荐
    • spring
      spring-framework-2.5.2.zip
    • spring
      春天 1,HelloSpring IOC创建对象spring1模块 2,依赖注入spring2模块 3,自动装配spring3模块 4,注释spring4模块 5,JavaConfig spring5模块
    • spring 集成
      spring 集成spring 集成spring 集成spring 集成spring 集成spring 集成spring 集成spring 集成spring 集成
    • springspring
      spring开发包 spring开发包 spring开发包 spring开发包
    • spring API
      spring API,Spring2.5-中文参考手册.chm,Spring3.0.2-RELEASE-API.chm,spring中文API.chm
    • spring 4.3.9
      spring 4-x: spring-framework-4.3.9.RELEASE-dist spring-framework-4.3.9.RELEASE-docs spring-framework-4.3.9.RELEASE-schema spring-framework-4.3.10.RELEASE-dist
    • spring 5.0.8
      spring 5-x: spring-framework-5.0.8.RELEASE-dist spring-framework-5.0.8.RELEASE-docs spring-framework-5.0.8.RELEASE-schema
    • spring2.5.6
      spring-bean-2.5.6.jar spring-context-2.5.6.jar spring-core-2.5.6.jar spring-web-2.5.6.jar
    • spring资源
      开发时可能需要用到spring资源,spring-context-3.1.1.RELEASE,spring-context-support-3.1.1.RELEASE,spring-core-3.1.1.RELEASE
    • Spring IoC
      实现Spring IoC功能的基本包,版本为3.2.8: commons-logging.jar spring-beans-3.2.8.RELEASE.jar spring-context-3.2.8.RELEASE.jar spring-core-3.2.8.RELEASE.jar spring-expression-3.2.8.RELEASE.jar