提交 899d235f 编写于 作者: 7 7wc98#14

add function

上级 b207f78f
......@@ -22,9 +22,14 @@
<!--阿里数据源依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!--<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
......
package com.pyc.campus.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @author 御承扬
* @product IntelliJ IDEA
* @project campus
* @file MyDatasourceConfig
* @pack com.pyc.campus.config
* @date 2021/2/8
* @time 8:40
* @E-mail 2923616405@qq.com
**/
@Configuration
public class MyDatasourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource(){
return new DruidDataSource();
}
@Bean
public ServletRegistrationBean<StatViewServlet> servletServletRegistrationBean(){
StatViewServlet servlet = new StatViewServlet();
return new ServletRegistrationBean<>(servlet, "/druid/*");
}
}
......@@ -7,6 +7,7 @@
package com.pyc.campus.config;
import com.pyc.campus.interceptor.AdminInterceptor;
import com.pyc.campus.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
......@@ -23,7 +24,12 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/login");
.addPathPatterns("/login")
.excludePathPatterns("/my/check","/my/**?**");
registry.addInterceptor(new AdminInterceptor())
.addPathPatterns("/manageUser","/toPublishQuestion","/toImportGrade","/publishNews")
.excludePathPatterns("/admin");
}
@Override
......
......@@ -57,6 +57,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/webjars/**").permitAll()
.antMatchers("/images/**/**").permitAll()
.antMatchers("/error/**").permitAll()
.antMatchers("/druid/**").permitAll()
.anyRequest().authenticated()
.and().formLogin()
.loginPage("/login")
......
......@@ -17,6 +17,7 @@ import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -66,6 +67,10 @@ public class WebController {
System.out.println(stuID + ", " + password + ", " + decodePassword);
Msg msg;
if(decodePassword.equals(password)){
Student student = studentRepository.findAllByStudentID(stuID);
if(student.getAdmin()==1){
session.setAttribute("admin",student.getAdmin());
}
session.setAttribute("loginUser",stuID);
msg = new Msg("提示","密码校验正确,请重新输入并单击登陆按钮进行登陆","");
model.addAttribute("msg",msg);
......@@ -186,10 +191,12 @@ public class WebController {
return "page/Sign";
}
@RequestMapping("/login")
@GetMapping("/login")
public String login() {
return "page/Login";
}
/*@RequestMapping("/toCheckFrozen")
public String toCheckFrozen(Model model)
{
......@@ -216,6 +223,8 @@ public class WebController {
model.addAttribute("msg", msg);
return "page/CheckFrozen";
}*/
/*@RequestMapping("/test")
public List<Grade> test(@Param("stuId")String studentId) {
return gradeRepository.findAllByStudentID(studentId);
......
......@@ -17,6 +17,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author 彭友聪
*/
public interface StudentRepository extends JpaRepository<Student,Long> {
Student findPasswordByStudentID(String studentID);
Student findNameByStudentID(String studentID);
......@@ -50,4 +53,7 @@ public interface StudentRepository extends JpaRepository<Student,Long> {
@Transactional
@Query("delete from Student where studentID=?1")
void delByStudentID(String studentId);
@Query("select s.admin from Student as s where s.studentID=?1")
int findAdminByStudentID(String studentID);
}
package com.pyc.campus.interceptor;
import com.pyc.campus.dao.StudentRepository;
import com.pyc.campus.domain.Msg;
import com.pyc.campus.domain.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author 御承扬
* @product IntelliJ IDEA
* @project campus
* @file AdminInterceptor
* @pack com.pyc.campus.interceptor
* @date 2021/2/8
* @time 13:23
* @E-mail 2923616405@qq.com
**/
@Slf4j
public class AdminInterceptor implements HandlerInterceptor {
/**
* 目标方法执行前
* @author 彭友聪
* @param request javax.servlet.http.HttpServletRequest
* @param response javax.servlet.http.HttpServletResponse
* @param handler Object
* @return boolean
* @throws Exception Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 登陆检查逻辑
log.info("preHandle拦截的请求路径是{}",request.getRequestURI());
HttpSession session = request.getSession();
Object admin = session.getAttribute("admin");
if(admin != null)
{
return true;
}else {
Msg msg = new Msg("系统警告","你不是管理员,不允许使用管理功能!","");
request.setAttribute("msg",msg);
request.getRequestDispatcher("/home").forward(request,response);
return false;
}
}
/**
* 目标方法执行后
* @author 彭友聪
* @param request javax.servlet.http.HttpServletRequest
* @param response javax.servlet.http.HttpServletResponse
* @param handler Object
* @param modelAndView org.springframework.web.servlet.ModelAndView
* @throws Exception Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.info("postHandle拦截的请求路径:{},{}",request.getRequestURI(),modelAndView);
}
/**
* 页面渲染后
* @author 彭友聪
* @param request javax.servlet.http.HttpServletRequest
* @param response javax.servlet.http.HttpServletResponse
* @param handler Object
* @param ex Exception
* @throws Exception Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
log.info("afterCompletion 拦截的请求路径:{},异常:{}", request.getRequestURI(),ex);
}
}
......@@ -8,6 +8,21 @@ spring.datasource.url=jdbc:mysql://localhost:3306/campus?serverTimezone=GMT%2B8
#spring.datasource.url=jdbc:mysql://rm-2ze0g228tqrrxny5u125010.mysql.rds.aliyuncs.com:3306/campus?serverTimezone=GMT%2B8
spring.datasource.username=pyc
spring.datasource.password=root19537
spring.datasource.druid.aop-patterns=com.pyc.campus.*
spring.datasource.druid.stat-view-servlet.allow=0.0.0.0
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false
#spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
#spring.jpa.open-in-view=true
spring.jpa.properties.hibernate.dialect = com.pyc.campus.config.MysqlConfig
......
spring:
resources:
cache:
period: 1100
\ No newline at end of file
period: 1100
datasource:
druid:
aop-patterns: com.pyc.campus.* #监控SpringBean
filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙)
stat-view-servlet: # 配置监控页功能
enabled: true
login-username: admin
login-password: admin
resetEnable: false
web-stat-filter: # 监控web
enabled: true
urlPattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat: # 对上面filters里面的stat的详细配置
slow-sql-millis: 1000
logSlowSql: true
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册