首页 > 代码库 > Online Judge(OJ)搭建——5、配置
Online Judge(OJ)搭建——5、配置
Spring
配置一些本地类,还有 HTML form 提交文件的解析器。
package per.piers.onlineJudge.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import javax.servlet.MultipartConfigElement; import javax.servlet.ServletRegistration; @Configuration public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class, SecurityConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setMultipartConfig(new MultipartConfigElement("")); } }
Spring Security
Spring 需要从数据库获取权限相关数据,所以需要配置数据源。之后根据相关用户身份,配置访问的权限。
package per.piers.onlineJudge.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import javax.sql.DataSource; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { private DataSource dataSource; @Autowired public SecurityConfig(DataSource dataSource) { this.dataSource = dataSource; } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication().dataSource(dataSource) .usersByUsernameQuery("SELECT email, password, enabled FROM users WHERE email=?") // password .authoritiesByUsernameQuery("SELECT email, role FROM users WHERE email=?"); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .authorizeRequests() // user .regexMatchers("/user/information").hasAnyAuthority("user","admin") // test .regexMatchers("/test/question/\\d+").hasAnyAuthority("user","admin") // score .regexMatchers("/score/.+").hasAnyAuthority("user","admin") // testManager .regexMatchers("/testManager/.+").hasAnyAuthority("admin") // others .anyRequest().permitAll().and() // set authorization matcher .formLogin().loginPage("/user/login").defaultSuccessUrl("/user/information").and() .rememberMe().tokenValiditySeconds(604800).key("OnlineJudge").and() .logout().logoutUrl("/user/logout").logoutSuccessUrl("/user/login"); } }
由于 Spring Security 的默认编码是 ISO-8850-1,所以需要配置 CharacterEncodingFilter 把默认编码变更为 UTF-8。
package per.piers.onlineJudge.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; import org.springframework.web.filter.CharacterEncodingFilter; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; @Configuration public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { @Override protected void beforeSpringSecurityFilterChain(ServletContext servletContext) { FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter()); characterEncodingFilter.setInitParameter("encoding", "UTF-8"); characterEncodingFilter.setInitParameter("forceEncoding", "true"); characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*"); } }
MyBatis
在Online Judge(OJ)搭建——2、数据库,SQL语句介绍过。
数据源
JNDI
在程序中,将数据源托管给专人负责,此为 JNDI 方式。这里托管给 Tomcat 负责,在OnlineJudge/WEB-INF/META-INF/context.xml配置。这种方式将程序和数据库链接解耦,推荐。
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="mybatis" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/online_judge?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=true" username="root" password="woaimysql-135" maxTotal="20" maxIdle="10" maxWaitMillis="10000"/> </Context>
Online Judge(OJ)搭建——5、配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。