├── .gitignore
├── README.md
├── pom.xml
├── src
├── main
│ ├── java
│ │ └── com
│ │ │ └── cc
│ │ │ ├── ManageBackendApplication.java
│ │ │ ├── config
│ │ │ ├── mvc
│ │ │ │ └── WebMvcConfig.java
│ │ │ ├── mybatis
│ │ │ │ └── MyBatisPlusConfig.java
│ │ │ └── security
│ │ │ │ ├── SpringSecurityConfig.java
│ │ │ │ ├── exception
│ │ │ │ └── CustomerAuthenionException.java
│ │ │ │ ├── filter
│ │ │ │ └── CheckTokenFilter.java
│ │ │ │ ├── handler
│ │ │ │ ├── CustomAccessDeineHandler.java
│ │ │ │ └── LoginFailureHandler.java
│ │ │ │ └── service
│ │ │ │ └── CustomUserDetailService.java
│ │ │ ├── controller
│ │ │ ├── BuildingController.java
│ │ │ ├── CommunityInfoController.java
│ │ │ ├── ComplaintController.java
│ │ │ ├── ElectricController.java
│ │ │ ├── FacilitiesController.java
│ │ │ ├── HouseController.java
│ │ │ ├── LiverController.java
│ │ │ ├── MenuController.java
│ │ │ ├── NoticeController.java
│ │ │ ├── ParkController.java
│ │ │ ├── ParkingController.java
│ │ │ ├── RecordsController.java
│ │ │ ├── RepairController.java
│ │ │ ├── ReportController.java
│ │ │ ├── RoleController.java
│ │ │ ├── UnitController.java
│ │ │ ├── UserController.java
│ │ │ ├── WaterController.java
│ │ │ └── WorkerController.java
│ │ │ ├── domain
│ │ │ ├── parm
│ │ │ │ ├── AssignHouseParm.java
│ │ │ │ ├── BuildingParm.java
│ │ │ │ ├── ComplaintParm.java
│ │ │ │ ├── ElectricParm.java
│ │ │ │ ├── FacilitiesParm.java
│ │ │ │ ├── HouseParm.java
│ │ │ │ ├── IndexNoticeParm.java
│ │ │ │ ├── LiverElectricParm.java
│ │ │ │ ├── LiverParkParm.java
│ │ │ │ ├── LiverParm.java
│ │ │ │ ├── LiverWaterParm.java
│ │ │ │ ├── LoginParm.java
│ │ │ │ ├── NoticeParm.java
│ │ │ │ ├── ParkParm.java
│ │ │ │ ├── ParkingParm.java
│ │ │ │ ├── RepairParm.java
│ │ │ │ ├── ResetPassParm.java
│ │ │ │ ├── RoleAssignParm.java
│ │ │ │ ├── RoleParm.java
│ │ │ │ ├── RolePermissionParm.java
│ │ │ │ ├── UnitParm.java
│ │ │ │ ├── UserParm.java
│ │ │ │ └── WaterParm.java
│ │ │ ├── pojo
│ │ │ │ ├── Building.java
│ │ │ │ ├── ChangePassword.java
│ │ │ │ ├── CommunityInfo.java
│ │ │ │ ├── Complaint.java
│ │ │ │ ├── Electric.java
│ │ │ │ ├── Facilities.java
│ │ │ │ ├── House.java
│ │ │ │ ├── Liver.java
│ │ │ │ ├── LiverAndHouse.java
│ │ │ │ ├── LiverAndParking.java
│ │ │ │ ├── LiverAndRole.java
│ │ │ │ ├── Menu.java
│ │ │ │ ├── Notice.java
│ │ │ │ ├── Park.java
│ │ │ │ ├── Parking.java
│ │ │ │ ├── Repair.java
│ │ │ │ ├── Role.java
│ │ │ │ ├── RoleAndMenu.java
│ │ │ │ ├── Unit.java
│ │ │ │ ├── User.java
│ │ │ │ ├── UserAndRole.java
│ │ │ │ ├── UserInfo.java
│ │ │ │ └── Water.java
│ │ │ ├── status
│ │ │ │ └── StatusCode.java
│ │ │ └── vo
│ │ │ │ ├── LoginResult.java
│ │ │ │ ├── ResultVo.java
│ │ │ │ ├── RolePermissionVo.java
│ │ │ │ └── RouterVO.java
│ │ │ ├── mapper
│ │ │ ├── BuildingMapper.java
│ │ │ ├── CommunityInfoMapper.java
│ │ │ ├── ComplaintMapper.java
│ │ │ ├── ElectricMapper.java
│ │ │ ├── FacilitiesMapper.java
│ │ │ ├── HouseMapper.java
│ │ │ ├── LiverAndHouseMapper.java
│ │ │ ├── LiverAndParkingMapper.java
│ │ │ ├── LiverAndRoleMapper.java
│ │ │ ├── LiverMapper.java
│ │ │ ├── MenuMapper.java
│ │ │ ├── NoticeMapper.java
│ │ │ ├── ParkMapper.java
│ │ │ ├── ParkingMapper.java
│ │ │ ├── RepairMapper.java
│ │ │ ├── RoleAndMenuMapper.java
│ │ │ ├── RoleMapper.java
│ │ │ ├── UnitMapper.java
│ │ │ ├── UserAndRoleMapper.java
│ │ │ ├── UserMapper.java
│ │ │ ├── WaterMapper.java
│ │ │ └── WorkerMapper.java
│ │ │ ├── service
│ │ │ ├── BuildingService.java
│ │ │ ├── CommunityInfoService.java
│ │ │ ├── ComplaintService.java
│ │ │ ├── ElectricService.java
│ │ │ ├── FacilitiesService.java
│ │ │ ├── HouseService.java
│ │ │ ├── LiverAndRoleService.java
│ │ │ ├── LiverService.java
│ │ │ ├── MenuService.java
│ │ │ ├── NoticeService.java
│ │ │ ├── ParkService.java
│ │ │ ├── ParkingService.java
│ │ │ ├── RepairService.java
│ │ │ ├── RoleAndMenuService.java
│ │ │ ├── RoleService.java
│ │ │ ├── UnitService.java
│ │ │ ├── UserAndRoleService.java
│ │ │ ├── UserService.java
│ │ │ ├── WaterService.java
│ │ │ ├── WorkerService.java
│ │ │ └── impl
│ │ │ │ ├── BuildingServiceImpl.java
│ │ │ │ ├── CommunityInfoServiceImpl.java
│ │ │ │ ├── ComplaintServiceImpl.java
│ │ │ │ ├── ElectricServiceImpl.java
│ │ │ │ ├── FacilitiesServiceImpl.java
│ │ │ │ ├── HouseServiceImpl.java
│ │ │ │ ├── LiverAndRoleServiceImpl.java
│ │ │ │ ├── LiverServiceImpl.java
│ │ │ │ ├── MenuServiceImpl.java
│ │ │ │ ├── NoticeServiceImpl.java
│ │ │ │ ├── ParkServiceImpl.java
│ │ │ │ ├── ParkingServiceImpl.java
│ │ │ │ ├── RepairServiceImpl.java
│ │ │ │ ├── RoleAndMenuServiceImpl.java
│ │ │ │ ├── RoleServiceImpl.java
│ │ │ │ ├── UnitServiceImpl.java
│ │ │ │ ├── UserAndRoleServiceImpl.java
│ │ │ │ ├── UserServiceImpl.java
│ │ │ │ ├── WaterServiceImpl.java
│ │ │ │ └── WorkerServiceImpl.java
│ │ │ └── utils
│ │ │ ├── DateUtils.java
│ │ │ ├── JwtUtils.java
│ │ │ ├── MyTreeUtils.java
│ │ │ ├── ResultUtils.java
│ │ │ ├── SMSUtils.java
│ │ │ └── ValidateCodeUtils.java
│ └── resources
│ │ ├── META-INF
│ │ └── additional-spring-configuration-metadata.json
│ │ ├── application.yml
│ │ ├── mapper
│ │ ├── ComplaintMapper.xml
│ │ ├── ElectricMapper.xml
│ │ ├── HouseMapper.xml
│ │ ├── LiverMapper.xml
│ │ ├── MenuMapper.xml
│ │ ├── NoticeMapper.xml
│ │ ├── ParkMapper.xml
│ │ ├── RepairMapper.xml
│ │ ├── RoleAndMenuMapper.xml
│ │ ├── UnitMapper.xml
│ │ └── WaterMapper.xml
│ │ └── system.sql
└── test
│ └── java
│ └── com
│ └── cc
│ └── ManageBackendApplicationTests.java
├── 前台.png
└── 后台.png
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 物业后台管理系统-后端
2 |
3 | ### 前端代码仓库链接
4 | [前端代码仓库](https://github.com/Pitayafruits/manage_front)
5 |
6 | ### 运行前配置
7 | 1. 在yml配置文件中修改自己的数据库配置
8 | 2. 在utils包下的SMSUtils下修改自己短信服务key
9 | 3. 数据库SQL文件在resources目录下
10 |
11 | ### 介绍
12 | 这是一款前后端分离的基于Spring Boot的物业管理系统,该系统实现了物业工作人员管理、业主管理、房屋管理、物业管理、人事管理、报修管理等主要功能。
13 |
14 | ### 软件架构
15 | * 核心框架:Spring Boot 2.3.7.RELEASE
16 | * 安全框架:Spring Security 5.3.6.RELEASE
17 | * 持久层框架:MyBatis-Plus 3.5.2
18 | * 关系型数据库: Mysql 5.7.35
19 | * 数据库连接池:Druid 1.2.11
20 | * 缓存数据库: Redis 4.0.9
21 | * 项目管理工具: Maven 3.8.4
22 |
23 | ### 界面展示
24 | 1. 前台页面展示
25 |
26 | 
27 |
28 | 2. 后台页面展示
29 |
30 | 
31 |
32 | ### 下一步工作
33 | * 在考虑把权限框架由Spring Security替换为更轻量的Sa-Token
34 | * 想把缴费管理改成定时任务那种批量自动生成账单的形式
35 |
36 | ### 感谢
37 | 感谢B站上那些分享无私技术的UP主,这个毕设项目对于我的提升主要在前端方面,在做它之前,自己还没有过独立完成前端项目的经验,而正是凭借B站上的那
38 | 些课程,也算磕磕绊绊地写出了自己满意的页面,这个项目也是给我的本科生涯画上了句号,希望自己的程序员生涯能够走得久一点吧!
39 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | com.cc
6 | manage_backend
7 | 0.0.1-SNAPSHOT
8 | manage_backend
9 | manage_backend
10 |
11 |
12 | 1.8
13 | UTF-8
14 | UTF-8
15 | 2.3.7.RELEASE
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 |
32 | com.alibaba
33 | fastjson
34 | 2.0.19.graal
35 |
36 |
37 |
38 |
39 | org.projectlombok
40 | lombok
41 | true
42 |
43 |
44 |
45 |
46 | mysql
47 | mysql-connector-java
48 | runtime
49 |
50 |
51 |
52 |
53 | com.alibaba
54 | druid-spring-boot-starter
55 | 1.2.11
56 |
57 |
58 |
59 |
60 | com.baomidou
61 | mybatis-plus-boot-starter
62 | 3.5.2
63 |
64 |
65 |
66 |
67 | org.springframework.boot
68 | spring-boot-devtools
69 | runtime
70 | true
71 |
72 |
73 |
74 |
75 | io.jsonwebtoken
76 | jjwt
77 | 0.9.0
78 |
79 |
80 |
81 |
82 | org.springframework.boot
83 | spring-boot-starter-security
84 |
85 |
86 |
87 |
88 | com.tencentcloudapi
89 | tencentcloud-sdk-java
90 | 3.1.270
91 |
92 |
93 |
94 |
95 | org.springframework.boot
96 | spring-boot-starter-data-redis
97 |
98 |
99 |
100 | org.springframework.boot
101 | spring-boot-starter-test
102 | test
103 |
104 |
105 | org.junit.vintage
106 | junit-vintage-engine
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 | org.springframework.boot
116 | spring-boot-dependencies
117 | ${spring-boot.version}
118 | pom
119 | import
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 | org.apache.maven.plugins
128 | maven-compiler-plugin
129 | 3.8.1
130 |
131 | 1.8
132 | 1.8
133 | UTF-8
134 |
135 |
136 |
137 | org.springframework.boot
138 | spring-boot-maven-plugin
139 | 2.3.7.RELEASE
140 |
141 | com.cc.ManageBackendApplication
142 |
143 |
144 |
145 | repackage
146 |
147 | repackage
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
--------------------------------------------------------------------------------
/src/main/java/com/cc/ManageBackendApplication.java:
--------------------------------------------------------------------------------
1 | package com.cc;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ManageBackendApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(ManageBackendApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/mvc/WebMvcConfig.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.mvc;
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 | @Configuration
8 | public class WebMvcConfig implements WebMvcConfigurer {
9 | /**
10 | * 跨域配置
11 | */
12 | @Override
13 | public void addCorsMappings(CorsRegistry registry) {
14 | registry.addMapping("/**")
15 | .allowCredentials(true)
16 | .allowedOrigins("*")
17 | .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
18 | .allowedHeaders("*")
19 | .exposedHeaders("*");
20 | }
21 | }
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/mybatis/MyBatisPlusConfig.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.mybatis;
2 |
3 | import com.baomidou.mybatisplus.annotation.DbType;
4 | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
5 | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
6 | import org.mybatis.spring.annotation.MapperScan;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | /**
11 | * mybatis-plus配置类
12 | */
13 | @Configuration
14 | @MapperScan("com.cc.mapper")
15 | public class MyBatisPlusConfig {
16 |
17 | //分页插件
18 | @Bean
19 | public MybatisPlusInterceptor mybatisPlusInterceptor(){
20 | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
21 | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
22 | return interceptor;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/SpringSecurityConfig.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security;
2 |
3 | import com.cc.config.security.filter.CheckTokenFilter;
4 | import com.cc.config.security.handler.CustomAccessDeineHandler;
5 | import com.cc.config.security.handler.LoginFailureHandler;
6 | import com.cc.config.security.service.CustomUserDetailService;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.security.authentication.AuthenticationManager;
11 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
13 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
15 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
16 | import org.springframework.security.config.http.SessionCreationPolicy;
17 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
18 | import org.springframework.security.crypto.password.PasswordEncoder;
19 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
20 |
21 | /**
22 | * SpringSecurity配置类
23 | */
24 |
25 | @Configuration
26 | @EnableWebSecurity //启用spring security
27 | @EnableGlobalMethodSecurity(prePostEnabled = true) //启用spring security注解
28 | public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
29 |
30 | @Autowired
31 | private CustomUserDetailService customUserDetailService;
32 |
33 | @Autowired
34 | private LoginFailureHandler loginFailureHandler;
35 |
36 | @Autowired
37 | private CustomAccessDeineHandler customAccessDeineHandler;
38 |
39 | @Autowired
40 | private CheckTokenFilter checkTokenFilter;
41 |
42 | @Bean
43 | public PasswordEncoder passwordEncoder(){
44 | return new BCryptPasswordEncoder();
45 | }
46 |
47 | //配置方法
48 | @Override
49 | protected void configure(HttpSecurity http) throws Exception {
50 | //解决跨域问题
51 | http.cors().and().headers().frameOptions().disable();
52 | http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);
53 | //关闭跨域请求伪造
54 | http.csrf().disable()
55 | //基于token,所以不需要session
56 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
57 | .and()
58 | //放行首页不需要认证的请求
59 | .authorizeRequests()
60 | .antMatchers("/user/login","/community/comminfo","/worker/workerList","/notice/getNoticeList","/facilities/getFacList","/liver/sendMsg","/liver/resetPassword").permitAll()
61 | .anyRequest().authenticated()
62 | .and().exceptionHandling().authenticationEntryPoint(loginFailureHandler).accessDeniedHandler(customAccessDeineHandler);
63 | }
64 |
65 | //注入AuthenticationManager
66 | @Bean
67 | @Override
68 | public AuthenticationManager authenticationManagerBean() throws Exception {
69 | return super.authenticationManagerBean();
70 | }
71 |
72 |
73 | //配置自定义的UserDetailService
74 | @Override
75 | protected void configure(AuthenticationManagerBuilder auth) throws Exception{
76 | auth.userDetailsService(customUserDetailService);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/exception/CustomerAuthenionException.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security.exception;
2 |
3 | import org.springframework.security.core.AuthenticationException;
4 |
5 | /**
6 | * 自定义异常
7 | */
8 | public class CustomerAuthenionException extends AuthenticationException {
9 | public CustomerAuthenionException(String msg) {
10 | super(msg);
11 | }
12 | }
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/filter/CheckTokenFilter.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security.filter;
2 |
3 | import com.baomidou.mybatisplus.core.toolkit.StringUtils;
4 | import com.cc.config.security.exception.CustomerAuthenionException;
5 | import com.cc.config.security.handler.LoginFailureHandler;
6 | import com.cc.config.security.service.CustomUserDetailService;
7 | import com.cc.utils.JwtUtils;
8 | import io.jsonwebtoken.Claims;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.beans.factory.annotation.Value;
11 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
12 | import org.springframework.security.core.AuthenticationException;
13 | import org.springframework.security.core.context.SecurityContextHolder;
14 | import org.springframework.security.core.userdetails.UserDetails;
15 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
16 | import org.springframework.stereotype.Component;
17 | import org.springframework.web.filter.OncePerRequestFilter;
18 |
19 | import javax.servlet.FilterChain;
20 | import javax.servlet.ServletException;
21 | import javax.servlet.http.HttpServletRequest;
22 | import javax.servlet.http.HttpServletResponse;
23 | import java.io.IOException;
24 |
25 | /**
26 | * token验证过滤器
27 | */
28 | @Component("checkTokenFilter")
29 | public class CheckTokenFilter extends OncePerRequestFilter {
30 |
31 | //重置密码地址
32 | @Value("${cc.resetPassUrl}")
33 | private String resetPassUrl;
34 |
35 | //发送短信地址
36 | @Value("${cc.sendMsgUrl}")
37 | private String sendMsgUrl;
38 |
39 | //首页设施列表
40 | @Value("${cc.facListUrl}")
41 | private String facListUrl;
42 |
43 | //登录请求的地址
44 | @Value("${cc.loginUrl}")
45 | private String loginUrl;
46 |
47 | //首页小区信息的地址
48 | @Value("${cc.commInfoUrl}")
49 | private String commInfoUrl;
50 |
51 | //工作人员列表信息的地址
52 | @Value("${cc.workerListUrl}")
53 | private String workerListUrl;
54 |
55 | //公告列表的地址
56 | @Value("${cc.noticeListUrl}")
57 | private String noticeListUrl;
58 |
59 | @Autowired
60 | private JwtUtils jwtUtils;
61 |
62 | @Autowired
63 | private CustomUserDetailService customUserDetailService;
64 |
65 | @Autowired
66 | private LoginFailureHandler loginFailureHandler;
67 |
68 | @Override
69 | protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
70 | try{
71 | //获取当前请求的地址
72 | String url = httpServletRequest.getRequestURI();
73 | //不需要token验证的请求
74 | //登录请求 小区基本信息请求
75 | if(!url.equals(loginUrl) && (!url.equals(commInfoUrl)) && (!url.equals(workerListUrl)) && (!url.equals(noticeListUrl)) && (!url.equals(facListUrl)) && (!url.equals(sendMsgUrl)) && (!url.equals(resetPassUrl))){
76 | //非登录做token验证
77 | tokenValidate(httpServletRequest);
78 | }
79 | }catch (AuthenticationException e){
80 | loginFailureHandler.commence(httpServletRequest,httpServletResponse,e);
81 | return;
82 | }
83 | filterChain.doFilter(httpServletRequest,httpServletResponse);
84 | }
85 |
86 | //token验证
87 | private void tokenValidate(HttpServletRequest httpServletRequest){
88 | //获取token
89 | String token = httpServletRequest.getHeader("token");
90 | if(StringUtils.isEmpty(token)){
91 | token = httpServletRequest.getParameter("token");
92 | }
93 | if(StringUtils.isEmpty(token)){
94 | throw new CustomerAuthenionException("未获取到token!");
95 | }
96 | //token获取到则解析
97 | String username = jwtUtils.getUsernameFromToken(token);
98 | if(StringUtils.isEmpty(username)){
99 | throw new CustomerAuthenionException("登录已过期,请重新登录!");
100 | }
101 | //获取用户类型
102 | Claims claims = jwtUtils.getClaimsFromToken(token);
103 | String userType = (String) claims.get("userType");
104 | //查询用户信息,交给Spring Security
105 | UserDetails details = customUserDetailService.loadUserByUsername(username + ":" + userType);
106 | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(details,null, details.getAuthorities());
107 | authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
108 | //设置上下文
109 | SecurityContextHolder.getContext().setAuthentication(authenticationToken);
110 | }
111 |
112 | }
113 |
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/handler/CustomAccessDeineHandler.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security.handler;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 | import com.alibaba.fastjson.serializer.SerializerFeature;
5 | import com.cc.domain.vo.ResultVo;
6 | import org.springframework.security.access.AccessDeniedException;
7 | import org.springframework.security.web.access.AccessDeniedHandler;
8 | import org.springframework.stereotype.Component;
9 |
10 | import javax.servlet.ServletException;
11 | import javax.servlet.ServletOutputStream;
12 | import javax.servlet.http.HttpServletRequest;
13 | import javax.servlet.http.HttpServletResponse;
14 | import java.io.IOException;
15 |
16 | /**
17 | * 无权限访问
18 | */
19 | @Component("customAccessDeineHandler")
20 | public class CustomAccessDeineHandler implements AccessDeniedHandler {
21 | @Override
22 | public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
23 | String res = JSONObject.toJSONString(new ResultVo(700,"无权限访问,请联系管理员!",null), SerializerFeature.DisableCircularReferenceDetect);
24 | //设置返回格式
25 | httpServletResponse.setContentType("application/json;charset=UTF-8");
26 | ServletOutputStream out = httpServletResponse.getOutputStream();
27 | out.write(res.getBytes("UTF-8"));
28 | out.flush();
29 | out.close();
30 | }
31 | }
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/handler/LoginFailureHandler.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security.handler;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 | import com.alibaba.fastjson.serializer.SerializerFeature;
5 | import com.cc.config.security.exception.CustomerAuthenionException;
6 | import com.cc.domain.vo.ResultVo;
7 | import org.springframework.security.authentication.*;
8 | import org.springframework.security.core.AuthenticationException;
9 | import org.springframework.security.web.AuthenticationEntryPoint;
10 | import org.springframework.stereotype.Component;
11 |
12 | import javax.servlet.ServletException;
13 | import javax.servlet.ServletOutputStream;
14 | import javax.servlet.http.HttpServletRequest;
15 | import javax.servlet.http.HttpServletResponse;
16 | import java.io.IOException;
17 |
18 | /**
19 | * 认证失败处理器
20 | * 目的:返回JSON格式数据
21 | */
22 |
23 | @Component("loginFailureHandler")
24 | public class LoginFailureHandler implements AuthenticationEntryPoint {
25 | @Override
26 | public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
27 | int code = 500;
28 | String str = "";
29 | if(e instanceof AccountExpiredException){
30 | str = "账户过期,登录失败!";
31 | }else if(e instanceof BadCredentialsException){
32 | str = "用户名或密码错误,登录失败!";
33 | }else if(e instanceof CredentialsExpiredException){
34 | str = "密码过期,登录失败!";
35 | }else if(e instanceof DisabledException){
36 | str = "账户被禁用,登录失败!";
37 | }else if(e instanceof LockedException){
38 | str = "账户被锁,登录失败!";
39 | }else if(e instanceof InternalAuthenticationServiceException){
40 | str = "账户不存在,登录失败!";
41 | }else if(e instanceof CustomerAuthenionException){
42 | code = 600;
43 | str = e.getMessage();
44 | }else if(e instanceof InsufficientAuthenticationException){
45 | str = "无权限访问资源!";
46 | }
47 | else{
48 | str = "登录失败!";
49 | }
50 | String res = JSONObject.toJSONString(new ResultVo(code,str,null), SerializerFeature.DisableCircularReferenceDetect);
51 | //设置返回格式
52 | httpServletResponse.setContentType("application/json;charset=UTF-8");
53 | ServletOutputStream out = httpServletResponse.getOutputStream();
54 | out.write(res.getBytes("UTF-8"));
55 | out.flush();
56 | out.close();
57 | }
58 | }
--------------------------------------------------------------------------------
/src/main/java/com/cc/config/security/service/CustomUserDetailService.java:
--------------------------------------------------------------------------------
1 | package com.cc.config.security.service;
2 |
3 | import com.cc.domain.pojo.Liver;
4 | import com.cc.domain.pojo.Menu;
5 | import com.cc.domain.pojo.User;
6 | import com.cc.service.LiverService;
7 | import com.cc.service.MenuService;
8 | import com.cc.service.UserService;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.security.core.GrantedAuthority;
11 | import org.springframework.security.core.authority.AuthorityUtils;
12 | import org.springframework.security.core.userdetails.UserDetails;
13 | import org.springframework.security.core.userdetails.UserDetailsService;
14 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
15 | import org.springframework.stereotype.Component;
16 |
17 | import java.util.List;
18 | import java.util.stream.Collectors;
19 |
20 | /**
21 | * 自定义UserDetailService
22 | */
23 | @Component("customUserDetailService")
24 | public class CustomUserDetailService implements UserDetailsService {
25 |
26 | @Autowired
27 | private UserService userService;
28 |
29 | @Autowired
30 | private LiverService liverService;
31 |
32 | @Autowired
33 | private MenuService menuService;
34 |
35 | @Override
36 | public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
37 | //获取用户类型 0:业主 1:物业
38 | int index = s.indexOf(":");
39 | //截取账号和用户类型
40 | String username = s.substring(0,index);
41 | String userType = s.substring(index + 1, s.length());
42 | //根据用户类型查询不同表
43 | //UserDetails user = null;
44 | if(userType.equals("0")){ //业主
45 | Liver liver = liverService.loadUser(username);
46 | if(liver == null){
47 | throw new UsernameNotFoundException("用户账号不存在!");
48 | }
49 | //查询业主权限
50 | List