zoukankan      html  css  js  c++  java
  • 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&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=true"
                  username="root"
                  password="woaimysql-135"
                  maxTotal="20"
                  maxIdle="10"
                  maxWaitMillis="10000"/>
    </Context>
  • 相关阅读:
    起步!!!
    最简单的窗口弹出程序
    起步!!!
    写一个顺序表模板,包括顺序表的基本功能,例如查找,插
    最简单的窗口弹出程序
    [导入]8000元投资股市16年变成4亿元 中国版巴菲特?
    [导入]看到男司机被一车女歹徒侮辱,我好后悔没有能力制止!
    [导入]黑社会内部报纸一份,请含泪观看
    [导入]月薪80000的卖毛片给我上了震撼一课
    [导入]索尼新品可以握着用的怪异鼠标
  • 原文地址:https://www.cnblogs.com/Piers/p/6947103.html
Copyright © 2011-2022 走看看