Skip to content

Commit

Permalink
改进文件范围权限模块
Browse files Browse the repository at this point in the history
  • Loading branch information
ouyangyi1998 committed Apr 30, 2021
1 parent cfd2524 commit f9ba0ce
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 55 deletions.
92 changes: 46 additions & 46 deletions src/main/java/com/centerm/fud_demo/config/HttpsConfig.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
package com.centerm.fud_demo.config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author Sheva
* @version 1.0
* @date 2020/1/23 下午2:45
*/
@Configuration
public class HttpsConfig {
@Bean
public TomcatServletWebServerFactory servletContainer(){
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}

@Bean
public Connector httpConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听到的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(443);
return connector;
}
}
//package com.centerm.fud_demo.config;
//
//import org.apache.catalina.Context;
//import org.apache.catalina.connector.Connector;
//import org.apache.tomcat.util.descriptor.web.SecurityCollection;
//import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
//import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
///**
// * @author Sheva
// * @version 1.0
// * @date 2020/1/23 下午2:45
// */
//@Configuration
//public class HttpsConfig {
// @Bean
// public TomcatServletWebServerFactory servletContainer(){
// TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
// @Override
// protected void postProcessContext(Context context) {
// SecurityConstraint constraint = new SecurityConstraint();
// constraint.setUserConstraint("CONFIDENTIAL");
// SecurityCollection collection = new SecurityCollection();
// collection.addPattern("/*");
// constraint.addCollection(collection);
// context.addConstraint(constraint);
// }
// };
// tomcat.addAdditionalTomcatConnectors(httpConnector());
// return tomcat;
// }
//
// @Bean
// public Connector httpConnector(){
// Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
// connector.setScheme("http");
// //Connector监听到的http的端口号
// connector.setPort(8080);
// connector.setSecure(false);
// //监听到http的端口号后转向到的https的端口号
// connector.setRedirectPort(443);
// return connector;
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ public AjaxReturnMsg toDelete(HttpServletRequest request) {
return msg;
}

@PostMapping("changeFileScopeToPublic")
@ResponseBody
public AjaxReturnMsg changeFileScopeToPublic(HttpServletRequest request) {
AjaxReturnMsg msg = new AjaxReturnMsg();
Long fileId=Long.parseLong(request.getParameter("fileId"));
log.info("Change file: " + fileId + " to public");
fileService.changeFileScopeToPublic(fileId);
msg.setFlag(Constants.SUCCESS);
msg.setMsg("文件范围修改为Public");
return msg;
}

@PostMapping("changeFileScopeToPrivate")
@ResponseBody
public AjaxReturnMsg changeFileScopeToPrivate(HttpServletRequest request) {
AjaxReturnMsg msg = new AjaxReturnMsg();
Long fileId=Long.parseLong(request.getParameter("fileId"));
log.info("Change file: " + fileId + " to private");
fileService.changeFileScopeToPrivate(fileId);
msg.setFlag(Constants.SUCCESS);
msg.setMsg("文件范围修改为Private");
return msg;
}


@PostMapping("getChart")
@ResponseBody
public List<Map<String,Object>> getChart(HttpServletRequest request)
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/centerm/fud_demo/dao/FileDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ public interface FileDao {
* @return
*/
List<FileRecord> getHotFile();

/**
* 把文件范围修改到public
* @param fileId 文件id
*/
void changeFileScopeToPublic(Long fileId);
/**
* 把文件范围修改到private
* @param fileId 文件id
*/
void changeFileScopeToPrivate(Long fileId);
}
12 changes: 12 additions & 0 deletions src/main/java/com/centerm/fud_demo/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,17 @@ public interface FileService {
* @return
*/
List<FileRecord> getHotFile();

/**
* 把文件范围修改到public
* @param fileId 文件id
*/
void changeFileScopeToPublic(Long fileId);
/**
* 把文件范围修改到private
* @param fileId 文件id
*/
void changeFileScopeToPrivate(Long fileId);

}

Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ public Integer getFileNumberById(Long userId) {
public List<FileRecord> getHotFile() {
return fileDao.getHotFile();
}

@Override
public void changeFileScopeToPublic(Long fileId) {
fileDao.changeFileScopeToPublic(fileId);
}

@Override
public void changeFileScopeToPrivate(Long fileId) {
fileDao.changeFileScopeToPrivate(fileId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager

shiroFilterFactoryBean.setLoginUrl("/user/toLogin");
shiroFilterFactoryBean.setUnauthorizedUrl("/user/toLogin");
filterMap.put("/user/login","kickout,anon");
shiroFilterFactoryBean.setSuccessUrl("/user/toLogin");
filterMap.put("/user/login","anon");
filterMap.put("/file/**","authc");
filterMap.put("/config/**", "anon");
filterMap.put("/css/**", "anon");
Expand All @@ -59,17 +60,16 @@ public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager
filterMap.put("/admin/**","authc");
filterMap.put("/user/toLogin/**","anon");
filterMap.put("/user/toRegister/**","anon");
filterMap.put("/user/login","anon");
filterMap.put("/question/**","authc");
filterMap.put("/comment/**","authc");
filterMap.put("/personal/**","authc");
filterMap.put("/publish/**","authc");
filterMap.put("/notification/**","authc");
filterMap.put("/index/**","authc");
filterMap.put("/user/register","anon");
filterMap.put("/user/**","user,kickout");
filterMap.put("/user/**","authc");
filterMap.put("/user/logout","logout");
filterMap.put("/","authc");
filterMap.put("/**","authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spring.servlet.multipart.max-request-size=100MB
spring.servlet.multipart.max-file-size=100MB

spring.profiles.active=product
spring.profiles.active=test


server.port=443
server.port=8088
server.tomcat.uri-encoding=utf-8


Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/mapper/FileMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
WHERE fud.file.id=#{param1}
</update>
<select id="getFileByUserId" parameterType="long" resultType="com.centerm.fud_demo.entity.FileRecord">
SELECT file.id, file.name, file.size, file.suffix, date_format(fud.file.create_time,'%Y-%m-%d') AS createTime
SELECT file.id, file.name, file.size, file.suffix, date_format(fud.file.create_time,'%Y-%m-%d') AS createTime,file.scope
FROM fud.file
WHERE fud.file.user_id=#{userId}
ORDER BY fud.file.create_time DESC
Expand Down Expand Up @@ -175,11 +175,22 @@
FROM file
WHERE user_id = #{userId}
</select>

<select id="getHotFile" resultType="com.centerm.fud_demo.entity.FileRecord">
SELECT f.id, f.name, f.size, f.suffix, date_format(f.create_time,'%Y-%m-%d %H:%i:%S') AS createTime
FROM file f
WHERE f.scope = 1
ORDER BY f.download_times DESC
LIMIT 10
</select>
<update id="changeFileScopeToPublic" parameterType="long">
UPDATE file
SET scope = 1
WHERE id = #{fileId}
</update>
<update id="changeFileScopeToPrivate" parameterType="long">
UPDATE file
SET scope = 0
WHERE id = #{fileId}
</update>
</mapper>
114 changes: 112 additions & 2 deletions src/main/resources/templates/user/filemanager.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<link rel="stylesheet" th:href="@{/css/utils/font-awesome/css/font-awesome.css}">
<link rel="stylesheet" th:href="@{/css/utils/animate.css}">
<link th:href="@{/css/utils/sweetalert2.min.css}" rel="stylesheet"/>
<link rel="stylesheet" th:href="@{/css/index/lc_switch.css}">
<script type="text/javascript" th:src="@{/js/utils/love.js}"></script>
<script th:src="@{/js/utils/sweetalert2.js}"></script>
<script type="text/javascript" th:src="@{/js/utils/jquery.js}"></script>
Expand Down Expand Up @@ -206,17 +207,19 @@ <h4 class="card-title"><span class="lstick"></span>File Management</h4>
<th>文件大小</th>
<th>文件类型</th>
<th>上传时间</th>
<th>文件权限</th>
<th>文件下载</th>
<th>文件删除</th>
</tr>
</thead>
<tbody>
<tr th:each="file:${fileList}">
<tr th:each="file:${fileList}" th:switch="${file.scope}">
<td th:text="${fileStat.index+1}"></td>
<td th:text="${file.name}"></td>
<td th:text="${file.size}"></td>
<td th:text="${file.suffix}"></td>
<td th:text="${file.createTime}"></td>
<td> <input autocomplete="off" class="lcs_check" name="check" th:checked="${file.scope}==0?'false':'true'" th:id="'chexkbox'+${file.id}" type="checkbox"/></td>
<td><a th:href="@{/download/toDownload(id=${file.id})}">下载</a></td>
<td><a onclick="delFile(this)" href="javascript:;" th:id="${file.id}">删除</a></td>
</tr>
Expand All @@ -230,13 +233,120 @@ <h4 class="card-title"><span class="lstick"></span>File Management</h4>
</div>
</div>
</div>
<script th:src="@{/js/utils/jquery.js}"></script>
<script th:src="@{/js/index/popper.min.js}"></script>
<script th:src="@{/js/utils/bootstrap.min.js}"></script>
<script th:src="@{/js/index/perfect-scrollbar.jquery.min.js}"></script>
<script th:src="@{/js/index/waves.js}"></script>
<script th:src="@{/js/index/sidebarmenu.js}"></script>
<script th:src="@{/js/index/custom.min.js}"></script>
<script th:src="@{/js/upload/lc_switch.js}"></script>
<script type="text/javascript">
$(document).ready(function (e) {
$('.lcs_check').lc_switch();
// triggered each time a field changes status
$('body').delegate('.lcs_check', 'lcs-statuschange', function() {
var status = ($(this).is(':checked')) ? 'checked' : 'unchecked';
console.log('field changed status: '+ status );
});

// triggered each time a field is checked
$('body').delegate('.lcs_check', 'lcs-on', function() {
var checkbox = $(this).attr("id");
var id = "#"+checkbox;
var fileId = checkbox.substring(8);
swal({
title: '文件范围改为公有?',
text: 'the information may be lost',
type: 'info',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!'
}).then(function(result) {
if (result===false){
window.location.href = "/user/filemanager";
}
if (result===true) {
$.ajax({
type: "POST",
url: "/file/changeFileScopeToPublic",
dataType: "json",
data: {
fileId:fileId,
},
success: function (data) {
if (data.flag == 1) {
swal(
'Update!',
'File scope update successfully.',
'success'
).then(function () {
});
} else {
swal({
text: data.msg,
type: "error",
confirm: 'ok',
confirmButtonColor: '#fd2c45',
}).then(
function () {
});
}
}
});

}
});
});


// triggered each time a is unchecked
$('body').delegate('.lcs_check', 'lcs-off', function() {
var checkbox = $(this).attr("id");
var fileId = checkbox.substring(8);
swal({
title: '文件范围改为私有?',
text: 'the information may be lost',
type: 'info',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!'
}).then(function(result) {
if (result===false){
window.location.href = "/user/filemanager";
}
if (result===true) {
$.ajax({
type: "POST",
url: "/file/changeFileScopeToPrivate",
dataType: "json",
data: {
fileId:fileId,
},
success: function (data) {
if (data.flag == 1) {
swal(
'Update!',
'File scope update successfully.',
'success'
);
} else {
swal({
text: data.msg,
type: "error",
confirm: 'ok',
confirmButtonColor: '#fd2c45',
});
}
}
});

}
});
});
});
</script>
</body>

</html>

0 comments on commit f9ba0ce

Please sign in to comment.