正在显示
7 个修改的文件
包含
134 行增加
和
13 行删除
| 1 | +package com.tianbo.warehouse.annotation; | ||
| 2 | + | ||
| 3 | +import java.lang.annotation.ElementType; | ||
| 4 | +import java.lang.annotation.Retention; | ||
| 5 | +import java.lang.annotation.RetentionPolicy; | ||
| 6 | +import java.lang.annotation.Target; | ||
| 7 | + | ||
| 8 | +@Retention(RetentionPolicy.RUNTIME) | ||
| 9 | +@Target(ElementType.METHOD) | ||
| 10 | +public @interface RequestRequire { | ||
| 11 | + /** | ||
| 12 | + * 请求当前接口所需要的参数,多个以小写的逗号隔开 | ||
| 13 | + * @return | ||
| 14 | + */ | ||
| 15 | + public String require() default ""; | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + *传递参数的对象类型 | ||
| 19 | + */ | ||
| 20 | + public Class<?> parameter() default Object.class; | ||
| 21 | +} | 
| 1 | +package com.tianbo.warehouse.annotation; | ||
| 2 | + | ||
| 3 | +import org.apache.commons.lang.StringUtils; | ||
| 4 | +import org.aspectj.lang.ProceedingJoinPoint; | ||
| 5 | +import org.aspectj.lang.annotation.Around; | ||
| 6 | +import org.aspectj.lang.annotation.Aspect; | ||
| 7 | +import org.aspectj.lang.annotation.Pointcut; | ||
| 8 | +import org.aspectj.lang.reflect.MethodSignature; | ||
| 9 | +import org.slf4j.Logger; | ||
| 10 | +import org.slf4j.LoggerFactory; | ||
| 11 | +import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint; | ||
| 12 | +import org.springframework.stereotype.Component; | ||
| 13 | + | ||
| 14 | +import java.lang.reflect.Field; | ||
| 15 | +import java.lang.reflect.Method; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * controller层增强类,用于检测字符参数为空的情况,为空如""和null 都返回Null,为以后逻辑适配用 | ||
| 19 | + * @author mrz | ||
| 20 | + * @date 9:52 2019/04/15 | ||
| 21 | + * @params pjp | ||
| 22 | + * @throws | ||
| 23 | + * @return java.lang.Object | ||
| 24 | + **/ | ||
| 25 | +@Component | ||
| 26 | +@Aspect | ||
| 27 | +public class RequestRequireAOP { | ||
| 28 | + | ||
| 29 | + private static final Logger logger = LoggerFactory.getLogger(RequestRequireAOP.class); | ||
| 30 | + | ||
| 31 | + static final String split = ","; | ||
| 32 | + | ||
| 33 | + @Pointcut("@annotation(com.tianbo.warehouse.annotation.RequestRequire)") | ||
| 34 | + public void controllerInteceptor() { | ||
| 35 | + | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Around("controllerInteceptor()") | ||
| 39 | + public Object around(ProceedingJoinPoint pjp) throws Throwable { | ||
| 40 | + | ||
| 41 | + // 获取注解的方法参数列表 | ||
| 42 | + Object[] args = pjp.getArgs(); | ||
| 43 | + | ||
| 44 | + // 获取被注解的方法 | ||
| 45 | + MethodInvocationProceedingJoinPoint mjp = (MethodInvocationProceedingJoinPoint) pjp; | ||
| 46 | + MethodSignature signature = (MethodSignature) mjp.getSignature(); | ||
| 47 | + Method method = signature.getMethod(); | ||
| 48 | + | ||
| 49 | + // 获取方法上的注解 | ||
| 50 | + RequestRequire require = method.getAnnotation(RequestRequire.class); | ||
| 51 | + | ||
| 52 | + for(int i =0;i<args.length; i++){ | ||
| 53 | + //class相等表示是同一个对象 | ||
| 54 | + if (args[i].getClass().getName().equals("java.lang.String")) { | ||
| 55 | + | ||
| 56 | + if (null==args[i] || ((String)args[i]).isEmpty()){ | ||
| 57 | + args[i] = null; | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + // 如果没有报错,放行 | ||
| 63 | + return pjp.proceed(args); | ||
| 64 | + } | ||
| 65 | +} | 
| @@ -2,6 +2,7 @@ package com.tianbo.warehouse.controller; | @@ -2,6 +2,7 @@ package com.tianbo.warehouse.controller; | ||
| 2 | 2 | ||
| 3 | import com.github.pagehelper.PageInfo; | 3 | import com.github.pagehelper.PageInfo; | 
| 4 | import com.tianbo.warehouse.annotation.LogAnnotation; | 4 | import com.tianbo.warehouse.annotation.LogAnnotation; | 
| 5 | +import com.tianbo.warehouse.annotation.RequestRequire; | ||
| 5 | import com.tianbo.warehouse.annotation.UserPasswordMd5; | 6 | import com.tianbo.warehouse.annotation.UserPasswordMd5; | 
| 6 | import com.tianbo.warehouse.controller.response.ResultJson; | 7 | import com.tianbo.warehouse.controller.response.ResultJson; | 
| 7 | import com.tianbo.warehouse.model.USERS; | 8 | import com.tianbo.warehouse.model.USERS; | 
| @@ -32,6 +33,7 @@ public class UserController { | @@ -32,6 +33,7 @@ public class UserController { | ||
| 32 | @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息") | 33 | @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息") | 
| 33 | @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页-当前页", required = false, dataType = "int",defaultValue = "1"), | 34 | @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页-当前页", required = false, dataType = "int",defaultValue = "1"), | 
| 34 | @ApiImplicitParam(name = "pageSize", value = "分页-每页显示多少条", required = false, dataType = "int",defaultValue = "5")}) | 35 | @ApiImplicitParam(name = "pageSize", value = "分页-每页显示多少条", required = false, dataType = "int",defaultValue = "5")}) | 
| 36 | + @RequestRequire() | ||
| 35 | @GetMapping("/list") | 37 | @GetMapping("/list") | 
| 36 | public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") | 38 | public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") | 
| 37 | int pageNum, | 39 | int pageNum, | 
| @@ -41,9 +43,6 @@ public class UserController { | @@ -41,9 +43,6 @@ public class UserController { | ||
| 41 | @RequestParam(value = "realname",required = false) String realname) | 43 | @RequestParam(value = "realname",required = false) String realname) | 
| 42 | { | 44 | { | 
| 43 | USERS user = new USERS(); | 45 | USERS user = new USERS(); | 
| 44 | - //前端input传过来的为空,需要判断下 | ||
| 45 | - username = username.isEmpty()?null:username; | ||
| 46 | - realname = realname.isEmpty()?null:realname; | ||
| 47 | user.setUsername(username); | 46 | user.setUsername(username); | 
| 48 | user.setRealname(realname); | 47 | user.setRealname(realname); | 
| 49 | return userService.selectAllUser(pageNum,pageSize,user); | 48 | return userService.selectAllUser(pageNum,pageSize,user); | 
| @@ -2,11 +2,8 @@ package com.tianbo.warehouse.security.config; | @@ -2,11 +2,8 @@ package com.tianbo.warehouse.security.config; | ||
| 2 | 2 | ||
| 3 | import com.netflix.discovery.converters.Auto; | 3 | import com.netflix.discovery.converters.Auto; | 
| 4 | import com.tianbo.warehouse.security.CustomUserDetailService; | 4 | import com.tianbo.warehouse.security.CustomUserDetailService; | 
| 5 | -import com.tianbo.warehouse.security.handel.MyAuthenticationAccessDeniedHandler; | ||
| 6 | -import com.tianbo.warehouse.security.handel.MyAuthenticationFailHandler; | ||
| 7 | -import com.tianbo.warehouse.security.handel.MyAuthenticationSuccessHandler; | 5 | +import com.tianbo.warehouse.security.handel.*; | 
| 8 | import com.tianbo.warehouse.security.MyFilterSecurityInterceptor; | 6 | import com.tianbo.warehouse.security.MyFilterSecurityInterceptor; | 
| 9 | -import com.tianbo.warehouse.security.handel.MyLogoutSuccessHandler; | ||
| 10 | import org.springframework.beans.factory.annotation.Autowired; | 7 | import org.springframework.beans.factory.annotation.Autowired; | 
| 11 | import org.springframework.beans.factory.annotation.Qualifier; | 8 | import org.springframework.beans.factory.annotation.Qualifier; | 
| 12 | import org.springframework.context.annotation.Configuration; | 9 | import org.springframework.context.annotation.Configuration; | 
| @@ -50,6 +47,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | @@ -50,6 +47,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
| 50 | @Autowired | 47 | @Autowired | 
| 51 | private MyLogoutSuccessHandler myLogoutSuccessHandler; | 48 | private MyLogoutSuccessHandler myLogoutSuccessHandler; | 
| 52 | 49 | ||
| 50 | + @Autowired | ||
| 51 | + private MyAuthenticationEntryPoint authenticationEntryPoint; | ||
| 52 | + | ||
| 53 | @Override | 53 | @Override | 
| 54 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { | 54 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { | 
| 55 | //user Details Service验证 | 55 | //user Details Service验证 | 
| @@ -90,7 +90,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | @@ -90,7 +90,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
| 90 | .permitAll() | 90 | .permitAll() | 
| 91 | // .successForwardUrl("/main") | 91 | // .successForwardUrl("/main") | 
| 92 | .and() | 92 | .and() | 
| 93 | - .exceptionHandling().accessDeniedHandler(myAuthenticationAccessDeniedHandler) | 93 | + .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).accessDeniedHandler(myAuthenticationAccessDeniedHandler) | 
| 94 | .and() | 94 | .and() | 
| 95 | .logout() | 95 | .logout() | 
| 96 | .logoutSuccessUrl("/?logout=true") | 96 | .logoutSuccessUrl("/?logout=true") | 
| 1 | package com.tianbo.warehouse.security.handel; | 1 | package com.tianbo.warehouse.security.handel; | 
| 2 | 2 | ||
| 3 | +import org.springframework.core.annotation.Order; | ||
| 3 | import org.springframework.security.access.AccessDeniedException; | 4 | import org.springframework.security.access.AccessDeniedException; | 
| 4 | import org.springframework.security.web.access.AccessDeniedHandler; | 5 | import org.springframework.security.web.access.AccessDeniedHandler; | 
| 5 | import org.springframework.stereotype.Component; | 6 | import org.springframework.stereotype.Component; | 
| @@ -15,6 +16,7 @@ import java.io.PrintWriter; | @@ -15,6 +16,7 @@ import java.io.PrintWriter; | ||
| 15 | * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | 16 | * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | 
| 16 | */ | 17 | */ | 
| 17 | @Component | 18 | @Component | 
| 19 | +@Order(1) | ||
| 18 | public class MyAuthenticationAccessDeniedHandler implements AccessDeniedHandler{ | 20 | public class MyAuthenticationAccessDeniedHandler implements AccessDeniedHandler{ | 
| 19 | 21 | ||
| 20 | @Override | 22 | @Override | 
| 1 | package com.tianbo.warehouse.security.handel; | 1 | package com.tianbo.warehouse.security.handel; | 
| 2 | 2 | ||
| 3 | +import com.alibaba.fastjson.JSONObject; | ||
| 4 | +import org.springframework.security.core.AuthenticationException; | ||
| 3 | import org.springframework.security.web.AuthenticationEntryPoint; | 5 | import org.springframework.security.web.AuthenticationEntryPoint; | 
| 6 | +import org.springframework.stereotype.Component; | ||
| 4 | 7 | ||
| 5 | -/**实现AuthenticationEntryPoint接口 | 8 | +import javax.servlet.ServletException; | 
| 9 | +import javax.servlet.http.HttpServletRequest; | ||
| 10 | +import javax.servlet.http.HttpServletResponse; | ||
| 11 | +import java.io.IOException; | ||
| 12 | +import java.io.PrintWriter; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + *实现AuthenticationEntryPoint接口 | ||
| 6 | * AuthenticationEntryPoint 用来解决匿名用户访问无权限资源时的异常 | 16 | * AuthenticationEntryPoint 用来解决匿名用户访问无权限资源时的异常 | 
| 7 | * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | 17 | * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | 
| 8 | */ | 18 | */ | 
| 9 | -public class MyAuthenticationEntryPoint { | ||
| 10 | -// response.setCharacterEncoding("utf-8"); | ||
| 11 | -// response.setContentType("text/javascript;charset=utf-8"); | ||
| 12 | -// response.getWriter().print(JSONObject.toJSONString(RestMsg.error("没有访问权限!"))); | 19 | +@Component | 
| 20 | +public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint{ | ||
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public void commence(HttpServletRequest request, | ||
| 24 | + HttpServletResponse response, | ||
| 25 | + AuthenticationException authException) throws IOException, ServletException{ | ||
| 26 | +// response.setContentType("application/json;charset=utf-8"); | ||
| 27 | +// PrintWriter out = response.getWriter(); | ||
| 28 | +// StringBuffer sb = new StringBuffer(); | ||
| 29 | +// sb.append("{\"status\":\"error\",\"msg\":\""); | ||
| 30 | +// | ||
| 31 | +// sb.append("未登陆!"); | ||
| 32 | +// | ||
| 33 | +// sb.append("\"}"); | ||
| 34 | +// out.write(sb.toString()); | ||
| 35 | +// out.flush(); | ||
| 36 | +// out.close(); | ||
| 37 | + | ||
| 38 | + response.setCharacterEncoding("utf-8"); | ||
| 39 | + response.setContentType("application/json;charset=utf-8"); | ||
| 40 | + response.sendError(401,"未登陆"); | ||
| 41 | +// response.getWriter().print(JSONObject.toJSONString(Status.error("没有访问权限!"))); | ||
| 42 | + | ||
| 43 | + } | ||
| 44 | + | ||
| 13 | 45 | ||
| 14 | } | 46 | } | 
| @@ -51,6 +51,8 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat | @@ -51,6 +51,8 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat | ||
| 51 | response.setContentType("application/json;charset=UTF-8"); | 51 | response.setContentType("application/json;charset=UTF-8"); | 
| 52 | response.setHeader("Access-Control-Allow-Origin","*"); | 52 | response.setHeader("Access-Control-Allow-Origin","*"); | 
| 53 | USERS loginedUser = (USERS) authentication.getPrincipal(); | 53 | USERS loginedUser = (USERS) authentication.getPrincipal(); | 
| 54 | + //返回前端的数据安全起见把password去掉 | ||
| 55 | + loginedUser.setPassword(null); | ||
| 54 | Map<String,Object> menuMap = permissionService.getUserMenus(loginedUser.getUserId()); | 56 | Map<String,Object> menuMap = permissionService.getUserMenus(loginedUser.getUserId()); | 
| 55 | response.getWriter().write(objectMapper.writeValueAsString(new AuthSuccessResponse(authentication,menuMap))); | 57 | response.getWriter().write(objectMapper.writeValueAsString(new AuthSuccessResponse(authentication,menuMap))); | 
| 56 | }else { | 58 | }else { | 
- 
请 注册 或 登录 后发表评论