正在显示
19 个修改的文件
包含
242 行增加
和
16 行删除
| @@ -103,6 +103,12 @@ | @@ -103,6 +103,12 @@ | ||
| 103 | <artifactId>ojdbc6</artifactId> | 103 | <artifactId>ojdbc6</artifactId> |
| 104 | <version>11.2.0.4.0-atlassian-hosted</version> | 104 | <version>11.2.0.4.0-atlassian-hosted</version> |
| 105 | </dependency> | 105 | </dependency> |
| 106 | + <!--分页插件--> | ||
| 107 | + <dependency> | ||
| 108 | + <groupId>com.github.pagehelper</groupId> | ||
| 109 | + <artifactId>pagehelper-spring-boot-starter</artifactId> | ||
| 110 | + <version>1.2.5</version> | ||
| 111 | + </dependency> | ||
| 106 | <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --> | 112 | <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --> |
| 107 | <dependency> | 113 | <dependency> |
| 108 | <groupId>org.dom4j</groupId> | 114 | <groupId>org.dom4j</groupId> |
readme.md
0 → 100644
| 1 | +**gitlab**: [http://118.31.66.166:zp260/imf_cloud_wearhouse.git](git@118.31.66.166:zp260/imf_cloud_wearhouse.git) | ||
| 2 | + | ||
| 3 | +# 项目描述 | ||
| 4 | +国际货运物流平台开发脚手架 | ||
| 5 | +#集成 | ||
| 6 | +* 已集成IMF,基于IMF的xml报文格式。在master的git主分支上 | ||
| 7 | + * 接收IMF的消息 | ||
| 8 | + * 报文类型识别 | ||
| 9 | + * 报文分类本地存储 | ||
| 10 | + * 报文发送 | ||
| 11 | +* 已集成spring SECURITY | ||
| 12 | + * 支持前后端分离 | ||
| 13 | + * 自定义权限角色管理 | ||
| 14 | + * url角色权限识别 | ||
| 15 | + * menu与权限关联 | ||
| 16 | +* 已集成mybatis、mybatisGenerator、pageHelper | ||
| 17 | +* 集成定时任务框架 | ||
| 18 | + * 目前在IMF框架中使用 | ||
| 19 | +* 集成Spring Cloud | ||
| 20 | + |
| @@ -3,6 +3,9 @@ package com.tianbo.warehouse.controller; | @@ -3,6 +3,9 @@ package com.tianbo.warehouse.controller; | ||
| 3 | import com.tianbo.warehouse.model.USERS; | 3 | import com.tianbo.warehouse.model.USERS; |
| 4 | import com.tianbo.warehouse.service.UserService; | 4 | import com.tianbo.warehouse.service.UserService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; | 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.security.core.context.SecurityContextHolder; | ||
| 7 | +import org.springframework.security.core.context.SecurityContextImpl; | ||
| 8 | +import org.springframework.security.core.userdetails.UserDetails; | ||
| 6 | import org.springframework.web.bind.annotation.GetMapping; | 9 | import org.springframework.web.bind.annotation.GetMapping; |
| 7 | import org.springframework.web.bind.annotation.RestController; | 10 | import org.springframework.web.bind.annotation.RestController; |
| 8 | 11 | ||
| @@ -15,7 +18,9 @@ public class AdminController { | @@ -15,7 +18,9 @@ public class AdminController { | ||
| 15 | UserService userService; | 18 | UserService userService; |
| 16 | 19 | ||
| 17 | @GetMapping("/admin") | 20 | @GetMapping("/admin") |
| 18 | - public List<USERS> admin(){ | ||
| 19 | - return userService.selectAllUser(); | 21 | + public String admin(){ |
| 22 | + return "admin"; | ||
| 20 | } | 23 | } |
| 24 | + | ||
| 25 | + | ||
| 21 | } | 26 | } |
| @@ -34,9 +34,8 @@ public class ImfLog { | @@ -34,9 +34,8 @@ public class ImfLog { | ||
| 34 | 34 | ||
| 35 | @RequestMapping("/logs") | 35 | @RequestMapping("/logs") |
| 36 | @ResponseBody | 36 | @ResponseBody |
| 37 | - public List<USERS> logs(){ | ||
| 38 | - List<USERS> usersList =userService.selectAllUser(); | ||
| 39 | - return usersList; | 37 | + public String logs(){ |
| 38 | + return "logs"; | ||
| 40 | } | 39 | } |
| 41 | } | 40 | } |
| 42 | 41 |
| @@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMapping; | @@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMapping; | ||
| 6 | @Controller | 6 | @Controller |
| 7 | public class Login { | 7 | public class Login { |
| 8 | 8 | ||
| 9 | - @RequestMapping("/login") | 9 | + @RequestMapping("/loginPage") |
| 10 | public String login(){ | 10 | public String login(){ |
| 11 | return "login"; | 11 | return "login"; |
| 12 | } | 12 | } |
| @@ -20,8 +20,7 @@ public class MainController { | @@ -20,8 +20,7 @@ public class MainController { | ||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | @GetMapping("/main") | 22 | @GetMapping("/main") |
| 23 | - public List<USERS> me(){ | ||
| 24 | - List<USERS> usersList =userService.selectAllUser(); | ||
| 25 | - return usersList; | 23 | + public String main(){ |
| 24 | + return "main"; | ||
| 26 | } | 25 | } |
| 27 | } | 26 | } |
| 1 | +package com.tianbo.warehouse.controller; | ||
| 2 | + | ||
| 3 | +import com.github.pagehelper.PageInfo; | ||
| 4 | +import com.tianbo.warehouse.model.USERS; | ||
| 5 | +import com.tianbo.warehouse.service.UserService; | ||
| 6 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 7 | +import org.springframework.security.core.context.SecurityContextHolder; | ||
| 8 | +import org.springframework.security.core.userdetails.UserDetails; | ||
| 9 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 10 | +import org.springframework.web.bind.annotation.RequestParam; | ||
| 11 | +import org.springframework.web.bind.annotation.RestController; | ||
| 12 | + | ||
| 13 | +import java.util.List; | ||
| 14 | + | ||
| 15 | +@RestController | ||
| 16 | +public class UserController { | ||
| 17 | + | ||
| 18 | + @Autowired | ||
| 19 | + UserService userService; | ||
| 20 | + | ||
| 21 | + @GetMapping("/user/list") | ||
| 22 | + public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") | ||
| 23 | + int pageNum, | ||
| 24 | + @RequestParam(value = "pageSize",required = false,defaultValue = "10") | ||
| 25 | + int pageSize){ | ||
| 26 | + return userService.selectAllUser(pageNum,pageSize); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + public String getusername(){ | ||
| 30 | + | ||
| 31 | + //通过session获取当前登录的用户信息 | ||
| 32 | + UserDetails userDetails =(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
| 33 | + return "欢迎回来:"+userDetails.getUsername(); | ||
| 34 | + } | ||
| 35 | +} |
| @@ -13,7 +13,7 @@ import java.util.Iterator; | @@ -13,7 +13,7 @@ import java.util.Iterator; | ||
| 13 | 13 | ||
| 14 | @Service | 14 | @Service |
| 15 | public class MyAccessDecisionManager implements AccessDecisionManager{ | 15 | public class MyAccessDecisionManager implements AccessDecisionManager{ |
| 16 | - /** | 16 | + /**这里没用AccessDecisionVoter访问投票管理,自定义用户的role_name与URL需要的ROLE_NAME对碰决定,参考资料:https://blog.csdn.net/kaikai8552/article/details/3965841 |
| 17 | * decide方法接收三个参数,decide 方法是判定是否拥有权限的决策方法 | 17 | * decide方法接收三个参数,decide 方法是判定是否拥有权限的决策方法 |
| 18 | * 其中第一个参数中保存了当前登录用户的角色信息,authentication 是释CustomUserService中循环添加到 GrantedAuthority 对象中的权限信息集合. | 18 | * 其中第一个参数中保存了当前登录用户的角色信息,authentication 是释CustomUserService中循环添加到 GrantedAuthority 对象中的权限信息集合. |
| 19 | * object 包含客户端发起的请求的requset信息,可转换为 HttpServletRequest request = ((FilterInvocation) object).getHttpRequest(); | 19 | * object 包含客户端发起的请求的requset信息,可转换为 HttpServletRequest request = ((FilterInvocation) object).getHttpRequest(); |
| @@ -35,6 +35,12 @@ public class MyAccessDecisionManager implements AccessDecisionManager{ | @@ -35,6 +35,12 @@ public class MyAccessDecisionManager implements AccessDecisionManager{ | ||
| 35 | for(Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext(); ) { | 35 | for(Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext(); ) { |
| 36 | c = iter.next(); | 36 | c = iter.next(); |
| 37 | needRole = c.getAttribute(); | 37 | needRole = c.getAttribute(); |
| 38 | + | ||
| 39 | + //如果URL需要的权限为匿名访问,返回 | ||
| 40 | + if(("ROLE_ANONYMOUS").equals(needRole.trim())){ | ||
| 41 | + return; | ||
| 42 | + } | ||
| 43 | + | ||
| 38 | //authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合 | 44 | //authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合 |
| 39 | for(GrantedAuthority ga : authentication.getAuthorities()) { | 45 | for(GrantedAuthority ga : authentication.getAuthorities()) { |
| 40 | if(needRole.trim().equals(ga.getAuthority())) { | 46 | if(needRole.trim().equals(ga.getAuthority())) { |
| @@ -57,6 +57,8 @@ public class MyInvocationSecurityMetadataSourceService implements FilterInvocati | @@ -57,6 +57,8 @@ public class MyInvocationSecurityMetadataSourceService implements FilterInvocati | ||
| 57 | * 此方法是为了判定用户请求的url 是否在权限表中, | 57 | * 此方法是为了判定用户请求的url 是否在权限表中, |
| 58 | * 如果在权限表中,则返回给 decide 方法, | 58 | * 如果在权限表中,则返回给 decide 方法, |
| 59 | * 用来判定用户是否有此权限。如果不在权限表中则放行。 | 59 | * 用来判定用户是否有此权限。如果不在权限表中则放行。 |
| 60 | + * 如果getAttributes(Object o)方法返回null的话,意味着当前这个请求不需要任何角色就能访问 | ||
| 61 | + * getAttributes(Object o)方法返回的集合最终会来到AccessDecisionManager类中 | ||
| 60 | */ | 62 | */ |
| 61 | @Override | 63 | @Override |
| 62 | public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException { | 64 | public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException { |
| 1 | +package com.tianbo.warehouse.security.config; | ||
| 2 | + | ||
| 3 | +import org.springframework.context.annotation.Configuration; | ||
| 4 | +import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
| 5 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
| 6 | +/** | ||
| 7 | + * 解决前后端分离跨域问题 | ||
| 8 | + */ | ||
| 9 | +@Configuration | ||
| 10 | +public class CorsConfig implements WebMvcConfigurer{ | ||
| 11 | + | ||
| 12 | + @Override | ||
| 13 | + public void addCorsMappings(CorsRegistry registry) { | ||
| 14 | + registry.addMapping("/**")//设置允许跨域的路径 | ||
| 15 | + .allowedOrigins("*")//设置允许跨域请求的域名 | ||
| 16 | + .allowCredentials(true)//是否允许证书 不再默认开启 | ||
| 17 | + .allowedMethods("GET", "POST", "PUT", "DELETE")//设置允许的方法 | ||
| 18 | + .maxAge(3600);//跨域允许时间 | ||
| 19 | + } | ||
| 20 | +} |
| 1 | package com.tianbo.warehouse.security.config; | 1 | package com.tianbo.warehouse.security.config; |
| 2 | 2 | ||
| 3 | +import com.netflix.discovery.converters.Auto; | ||
| 4 | +import com.tianbo.warehouse.security.handel.MyAuthenticationAccessDeniedHandler; | ||
| 3 | import com.tianbo.warehouse.security.handel.MyAuthenticationFailHandler; | 5 | import com.tianbo.warehouse.security.handel.MyAuthenticationFailHandler; |
| 4 | import com.tianbo.warehouse.security.handel.MyAuthenticationSuccessHandler; | 6 | import com.tianbo.warehouse.security.handel.MyAuthenticationSuccessHandler; |
| 5 | import com.tianbo.warehouse.security.MyFilterSecurityInterceptor; | 7 | import com.tianbo.warehouse.security.MyFilterSecurityInterceptor; |
| 8 | +import com.tianbo.warehouse.security.handel.MyLogoutSuccessHandler; | ||
| 6 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.beans.factory.annotation.Qualifier; | 10 | import org.springframework.beans.factory.annotation.Qualifier; |
| 8 | import org.springframework.context.annotation.Configuration; | 11 | import org.springframework.context.annotation.Configuration; |
| 9 | import org.springframework.core.annotation.Order; | 12 | import org.springframework.core.annotation.Order; |
| 10 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | 13 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 14 | +import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication; | ||
| 15 | +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
| 11 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; | 16 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 12 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | 17 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 13 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | 18 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 14 | import org.springframework.security.core.userdetails.UserDetailsService; | 19 | import org.springframework.security.core.userdetails.UserDetailsService; |
| 15 | import org.springframework.security.crypto.password.PasswordEncoder; | 20 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 21 | +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; | ||
| 22 | +import org.springframework.web.cors.CorsUtils; | ||
| 16 | 23 | ||
| 17 | @Configuration | 24 | @Configuration |
| 18 | @EnableWebSecurity | 25 | @EnableWebSecurity |
| 26 | +@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
| 19 | @Order(1) | 27 | @Order(1) |
| 20 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | 28 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 21 | 29 | ||
| @@ -35,18 +43,36 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | @@ -35,18 +43,36 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
| 35 | @Autowired | 43 | @Autowired |
| 36 | private UserDetailsService userDetailsService; | 44 | private UserDetailsService userDetailsService; |
| 37 | 45 | ||
| 46 | + @Autowired | ||
| 47 | + private MyAuthenticationAccessDeniedHandler myAuthenticationAccessDeniedHandler; | ||
| 48 | + | ||
| 49 | + @Autowired | ||
| 50 | + private MyLogoutSuccessHandler myLogoutSuccessHandler; | ||
| 51 | + | ||
| 38 | @Override | 52 | @Override |
| 39 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { | 53 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 40 | //user Details Service验证 | 54 | //user Details Service验证 |
| 41 | auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); | 55 | auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); |
| 42 | } | 56 | } |
| 43 | 57 | ||
| 58 | + /** | ||
| 59 | + * 在configure(HttpSecurity http)方法中, | ||
| 60 | + * 通过withObjectPostProcessor将刚刚创建的UrlFilterInvocationSecurityMetadataSource和UrlAccessDecisionManager注入进来。 | ||
| 61 | + * 到时候,请求都会经过刚才的过滤器(除了configure(WebSecurity web)方法忽略的请求)。 | ||
| 62 | + * 通过myFilterSecurityInterceptor关联他俩 | ||
| 63 | + * @param http | ||
| 64 | + * @throws Exception | ||
| 65 | + */ | ||
| 44 | @Override | 66 | @Override |
| 45 | protected void configure(HttpSecurity http) throws Exception { | 67 | protected void configure(HttpSecurity http) throws Exception { |
| 46 | http.authorizeRequests() | 68 | http.authorizeRequests() |
| 69 | + //跨域配置 | ||
| 70 | + .requestMatchers(CorsUtils::isPreFlightRequest).permitAll() | ||
| 71 | + //管理页面只允许管理员角色访问 | ||
| 47 | .antMatchers("/admin","/role").authenticated() | 72 | .antMatchers("/admin","/role").authenticated() |
| 48 | - //管理页面只允许管理员角色访问 //任何请求,登录后可以访问 | ||
| 49 | - .anyRequest().permitAll() //其余的不需要验证 | 73 | + //任何请求,登录后可以访问 |
| 74 | + //其余的不需要验证 | ||
| 75 | + .anyRequest().permitAll() | ||
| 50 | .and() | 76 | .and() |
| 51 | .formLogin() | 77 | .formLogin() |
| 52 | .passwordParameter("password") | 78 | .passwordParameter("password") |
| @@ -61,8 +87,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | @@ -61,8 +87,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
| 61 | .permitAll() | 87 | .permitAll() |
| 62 | // .successForwardUrl("/main") | 88 | // .successForwardUrl("/main") |
| 63 | .and() | 89 | .and() |
| 90 | + .exceptionHandling().accessDeniedHandler(myAuthenticationAccessDeniedHandler) | ||
| 91 | + .and() | ||
| 64 | .logout() | 92 | .logout() |
| 65 | .logoutSuccessUrl("/?logout=true") | 93 | .logoutSuccessUrl("/?logout=true") |
| 94 | + .logoutSuccessHandler(myLogoutSuccessHandler) | ||
| 66 | .permitAll() | 95 | .permitAll() |
| 67 | .and() | 96 | .and() |
| 68 | .rememberMe() | 97 | .rememberMe() |
| @@ -77,6 +106,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | @@ -77,6 +106,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
| 77 | .and() | 106 | .and() |
| 78 | .csrf().disable(); | 107 | .csrf().disable(); |
| 79 | 108 | ||
| 80 | - //http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class).csrf().disable(); | 109 | + http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class); |
| 81 | } | 110 | } |
| 82 | } | 111 | } |
src/main/java/com/tianbo/warehouse/security/handel/MyAuthenticationAccessDeniedHandler.java
0 → 100644
| 1 | +package com.tianbo.warehouse.security.handel; | ||
| 2 | + | ||
| 3 | +import org.springframework.security.access.AccessDeniedException; | ||
| 4 | +import org.springframework.security.web.access.AccessDeniedHandler; | ||
| 5 | +import org.springframework.stereotype.Component; | ||
| 6 | + | ||
| 7 | +import javax.servlet.ServletException; | ||
| 8 | +import javax.servlet.http.HttpServletRequest; | ||
| 9 | +import javax.servlet.http.HttpServletResponse; | ||
| 10 | +import java.io.IOException; | ||
| 11 | +import java.io.PrintWriter; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * AuthenticationEntryPoint 用来解决匿名用户访问无权限资源时的异常 | ||
| 15 | + * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | ||
| 16 | + */ | ||
| 17 | +@Component | ||
| 18 | +public class MyAuthenticationAccessDeniedHandler implements AccessDeniedHandler{ | ||
| 19 | + | ||
| 20 | + @Override | ||
| 21 | + public void handle(HttpServletRequest httpServletRequest, HttpServletResponse resp, AccessDeniedException e) throws IOException, ServletException{ | ||
| 22 | + | ||
| 23 | + resp.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
| 24 | + resp.setCharacterEncoding("UTF-8"); | ||
| 25 | + PrintWriter out = resp.getWriter(); | ||
| 26 | + out.write("{\"status\":\"error\",\"msg\":\"权限不足,请联系管理员!\"}"); | ||
| 27 | + out.flush(); | ||
| 28 | + out.close(); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | +} |
| 1 | +package com.tianbo.warehouse.security.handel; | ||
| 2 | + | ||
| 3 | +import org.springframework.security.web.AuthenticationEntryPoint; | ||
| 4 | + | ||
| 5 | +/**实现AuthenticationEntryPoint接口 | ||
| 6 | + * AuthenticationEntryPoint 用来解决匿名用户访问无权限资源时的异常 | ||
| 7 | + * AccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常 | ||
| 8 | + */ | ||
| 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("没有访问权限!"))); | ||
| 13 | + | ||
| 14 | +} |
| @@ -7,7 +7,10 @@ import org.apache.commons.logging.Log; | @@ -7,7 +7,10 @@ import org.apache.commons.logging.Log; | ||
| 7 | import org.apache.commons.logging.LogFactory; | 7 | import org.apache.commons.logging.LogFactory; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.http.HttpStatus; | 9 | import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.security.authentication.BadCredentialsException; | ||
| 11 | +import org.springframework.security.authentication.DisabledException; | ||
| 10 | import org.springframework.security.core.AuthenticationException; | 12 | import org.springframework.security.core.AuthenticationException; |
| 13 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
| 11 | import org.springframework.security.web.DefaultRedirectStrategy; | 14 | import org.springframework.security.web.DefaultRedirectStrategy; |
| 12 | import org.springframework.security.web.RedirectStrategy; | 15 | import org.springframework.security.web.RedirectStrategy; |
| 13 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; | 16 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
| @@ -17,6 +20,7 @@ import javax.servlet.ServletException; | @@ -17,6 +20,7 @@ import javax.servlet.ServletException; | ||
| 17 | import javax.servlet.http.HttpServletRequest; | 20 | import javax.servlet.http.HttpServletRequest; |
| 18 | import javax.servlet.http.HttpServletResponse; | 21 | import javax.servlet.http.HttpServletResponse; |
| 19 | import java.io.IOException; | 22 | import java.io.IOException; |
| 23 | +import java.io.PrintWriter; | ||
| 20 | 24 | ||
| 21 | /** | 25 | /** |
| 22 | * 自定义登录失败处理器 | 26 | * 自定义登录失败处理器 |
| @@ -41,7 +45,25 @@ public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureH | @@ -41,7 +45,25 @@ public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureH | ||
| 41 | @Override | 45 | @Override |
| 42 | public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { | 46 | public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { |
| 43 | 47 | ||
| 48 | + //返回前端原因 | ||
| 49 | + PrintWriter out = response.getWriter(); | ||
| 50 | + StringBuffer sb = new StringBuffer(); | ||
| 51 | + sb.append("{\"status\":\"error\",\"msg\":\""); | ||
| 52 | + if (exception instanceof UsernameNotFoundException || exception instanceof BadCredentialsException) { | ||
| 53 | + sb.append("用户名或密码输入错误,登录失败!"); | ||
| 54 | + } else if (exception instanceof DisabledException) { | ||
| 55 | + sb.append("账户被禁用,登录失败,请联系管理员!"); | ||
| 56 | + } else { | ||
| 57 | + sb.append("登录失败!"); | ||
| 58 | + } | ||
| 59 | + sb.append("\"}"); | ||
| 60 | +// out.write(sb.toString()); | ||
| 61 | +// out.flush(); | ||
| 62 | +// out.close(); | ||
| 63 | + | ||
| 44 | logger.info("登录失败"); | 64 | logger.info("登录失败"); |
| 65 | + | ||
| 66 | + //不返回具体原因 只返回异常 | ||
| 45 | //如果securityProperties中配置的是JSON就返回JSON | 67 | //如果securityProperties中配置的是JSON就返回JSON |
| 46 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ | 68 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ |
| 47 | //设置状态码 | 69 | //设置状态码 |
| @@ -41,6 +41,7 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat | @@ -41,6 +41,7 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat | ||
| 41 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ | 41 | if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){ |
| 42 | //将 authention 信息打包成json格式返回 | 42 | //将 authention 信息打包成json格式返回 |
| 43 | response.setContentType("application/json;charset=UTF-8"); | 43 | response.setContentType("application/json;charset=UTF-8"); |
| 44 | + response.setHeader("Access-Control-Allow-Origin","*"); | ||
| 44 | response.getWriter().write(objectMapper.writeValueAsString(authentication)); | 45 | response.getWriter().write(objectMapper.writeValueAsString(authentication)); |
| 45 | }else { | 46 | }else { |
| 46 | //走原来的处理流程 | 47 | //走原来的处理流程 |
| 1 | +package com.tianbo.warehouse.security.handel; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 4 | +import org.springframework.security.core.Authentication; | ||
| 5 | +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; | ||
| 6 | +import org.springframework.stereotype.Component; | ||
| 7 | + | ||
| 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 | +@Component | ||
| 15 | +public class MyLogoutSuccessHandler implements LogoutSuccessHandler { | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public void onLogoutSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException{ | ||
| 19 | + resp.setContentType("application/json;charset=utf-8"); | ||
| 20 | +// RespBean respBean = RespBean.ok("注销成功!"); | ||
| 21 | + ObjectMapper om = new ObjectMapper(); | ||
| 22 | + PrintWriter out = resp.getWriter(); | ||
| 23 | + out.write(om.writeValueAsString(authentication)); | ||
| 24 | + out.flush(); | ||
| 25 | + out.close(); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | +} |
| 1 | package com.tianbo.warehouse.service; | 1 | package com.tianbo.warehouse.service; |
| 2 | 2 | ||
| 3 | +import com.github.pagehelper.PageInfo; | ||
| 3 | import com.tianbo.warehouse.model.USERS; | 4 | import com.tianbo.warehouse.model.USERS; |
| 4 | 5 | ||
| 5 | import java.util.List; | 6 | import java.util.List; |
| 6 | 7 | ||
| 7 | public interface UserService { | 8 | public interface UserService { |
| 8 | USERS loadByUsername(String username); | 9 | USERS loadByUsername(String username); |
| 9 | - List<USERS> selectAllUser(); | 10 | + PageInfo<USERS> selectAllUser(int pageNum, int pageSize); |
| 10 | } | 11 | } |
| 1 | package com.tianbo.warehouse.service.imp; | 1 | package com.tianbo.warehouse.service.imp; |
| 2 | 2 | ||
| 3 | +import com.github.pagehelper.Page; | ||
| 4 | +import com.github.pagehelper.PageHelper; | ||
| 5 | +import com.github.pagehelper.PageInfo; | ||
| 3 | import com.tianbo.warehouse.dao.PERMISSIONMapper; | 6 | import com.tianbo.warehouse.dao.PERMISSIONMapper; |
| 4 | import com.tianbo.warehouse.dao.ROLEMapper; | 7 | import com.tianbo.warehouse.dao.ROLEMapper; |
| 5 | import com.tianbo.warehouse.dao.USERSMapper; | 8 | import com.tianbo.warehouse.dao.USERSMapper; |
| @@ -47,7 +50,8 @@ public class UserServiceImpl implements UserService{ | @@ -47,7 +50,8 @@ public class UserServiceImpl implements UserService{ | ||
| 47 | } | 50 | } |
| 48 | 51 | ||
| 49 | @Override | 52 | @Override |
| 50 | - public List<USERS> selectAllUser(){ | 53 | + public PageInfo<USERS> selectAllUser(int pageNum, int pageSize){ |
| 54 | + Page<USERS> page = PageHelper.startPage(pageNum,pageSize); | ||
| 51 | List<USERS> list = usersMapper.selectAllUser(); | 55 | List<USERS> list = usersMapper.selectAllUser(); |
| 52 | for (USERS user: list) { | 56 | for (USERS user: list) { |
| 53 | List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId()); | 57 | List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId()); |
| @@ -55,6 +59,7 @@ public class UserServiceImpl implements UserService{ | @@ -55,6 +59,7 @@ public class UserServiceImpl implements UserService{ | ||
| 55 | List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); | 59 | List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); |
| 56 | user.setRoles(roleList); | 60 | user.setRoles(roleList); |
| 57 | } | 61 | } |
| 58 | - return list; | 62 | + PageInfo<USERS> result = new PageInfo<USERS>(list); |
| 63 | + return result; | ||
| 59 | } | 64 | } |
| 60 | } | 65 | } |
| @@ -8,6 +8,9 @@ server.servlet.context-path=${SERVER_CONTEXTPATH:} | @@ -8,6 +8,9 @@ server.servlet.context-path=${SERVER_CONTEXTPATH:} | ||
| 8 | spring.application.name=tianbo.base.dev.devkit | 8 | spring.application.name=tianbo.base.dev.devkit |
| 9 | 9 | ||
| 10 | spring.jackson.serialization.fail-on-empty-beans=false | 10 | spring.jackson.serialization.fail-on-empty-beans=false |
| 11 | +#springboot2.0之后会把Date类型字段自动给转成UTC字符串 如:1990-11-26T16:00:00.000+0000,如果想转成时间戳在application.properties配置文件增加以下配置 | ||
| 12 | +spring.jackson.serialization.write-dates-as-timestamps=true | ||
| 13 | +spring.jackson.time-zone=GMT+8 | ||
| 11 | 14 | ||
| 12 | 15 | ||
| 13 | #springcloud 基本配置 | 16 | #springcloud 基本配置 |
-
请 注册 或 登录 后发表评论