org.jetbrains.kotlin.frontend和webpack,如何使用React-Router友好的URL

时间:2018-11-19 21:52:55

标签: webpack react-router kotlin-js

目前有一个不错的Kotlin react项目。 React-router是一种工作方式。我可以轻松点击以下网址: http://localhost:8088/#/blogPosts

但是如果删除并尝试点击: http://localhost:8088/blogPosts

我最终得到了: 美国东部时间2018年11月19日星期一16:48:31 发生意外错误(类型=未经授权,状态= 401)。 抱歉,您无权访问此资源。

这是我在产品的Web部分上的build.gradle:

apply plugin: 'org.jetbrains.kotlin.frontend'
apply plugin: 'kotlin2js'
apply plugin: 'kotlinx-serialization'

kotlinFrontend {
    downloadNodeJsVersion = '10.0.0'

    npm {
        dependency("webpack-cli", "v2.0.12")
        dependency("react", "16.6.0")
        dependency("react-dom", "16.6.0")
        dependency("react-router-dom", "4.3.1")
        dependency("axios", "0.18.0")
        dependency("react-modal", "^3.6.1")
        dependency("react-quill", "^1.3.2")
        dependency("html-react-parser", "^0.4.7")
    }

    sourceMaps = true

    webpackBundle {
        bundleName = "main"
        sourceMapEnabled = true
        contentPath = file('src/main/web')
        proxyUrl = "http://localhost:8080"
    }
}

dependencies {
    compile project(':common-js')
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
    compile("org.jetbrains.kotlinx:kotlinx-html-js:0.6.11") { force = true }
    compile 'org.jetbrains:kotlin-react-router-dom:4.3.1-pre.59-kotlin-1.3.0'
    compile "org.jetbrains:kotlin-react:16.5.2-pre.57-kotlin-1.2.71"
    compile "org.jetbrains:kotlin-react-dom:16.4.1-pre.34-kotlin-1.2.50"
}

compileKotlin2Js {
    kotlinOptions.metaInfo = true
    kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
    kotlinOptions.sourceMap = true
    kotlinOptions.moduleKind = 'commonjs'
    kotlinOptions.main = "call"
}

我正在项目的服务器部分上使用spring安全性,但这仅能处理:8080(我相信)

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
class SecurityConfig : WebSecurityConfigurerAdapter() {
    @Autowired
    internal lateinit var securityService: SecurityService //todo remove internal

    @Autowired
    private val unauthorizedHandler: JwtAuthenticationEntryPoint? = null

    @Bean
    fun jwtAuthenticationFilter(): JwtAuthenticationFilter {
        return JwtAuthenticationFilter()
    }

    @Throws(Exception::class)
    public override fun configure(authenticationManagerBuilder: AuthenticationManagerBuilder?) {
        authenticationManagerBuilder!!
                .userDetailsService<SecurityService>(securityService)
                .passwordEncoder(passwordEncoder())
    }

    @Bean(BeanIds.AUTHENTICATION_MANAGER)
    @Throws(Exception::class)
    override fun authenticationManagerBean(): AuthenticationManager {
        return super.authenticationManagerBean()
    }

    @Bean
    fun passwordEncoder(): PasswordEncoder {
        return BCryptPasswordEncoder()
    }

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http
                .cors()
                .and()
                .csrf()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/",
                        "/favicon.ico",
                        "/**/*.png",
                        "/**/*.gif",
                        "/**/*.svg",
                        "/**/*.jpg",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js")
                .permitAll()
                .antMatchers("/api/auth/**")
                .permitAll()
                .antMatchers("/api/secUser/checkUsernameAvailability", "/api/secUser/checkEmailAvailability")
                .permitAll()
                .antMatchers(HttpMethod.GET, "/api/polls/**", "/api/users/**")
                .permitAll()
                .anyRequest()
                .authenticated()

        // Add our custom JWT security filter
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter::class.java)

    }

    @Bean
    fun corsConfigurationSource(): CorsConfigurationSource {
        val configuration = CorsConfiguration()
        configuration.allowedOrigins = listOf("*")
        configuration.allowedMethods = listOf("GET", "POST", "PUT", "DELETE")
        configuration.allowedHeaders = listOf("*")
        val source = UrlBasedCorsConfigurationSource()
        source.registerCorsConfiguration("/**", configuration)
        return source
    }
}

0 个答案:

没有答案