Merge pull request 'tibor' (#15) from tibor into master

Reviewed-on: https://git.workouttest.org/bossanyit/aitrainer_server/pulls/15
This commit is contained in:
bossanyit 2023-03-29 18:43:49 +00:00
commit 210ff64c66
9 changed files with 57 additions and 20 deletions

View File

@ -42,7 +42,8 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
@Secured @Secured
@GetMapping("/customers/{id}") @GetMapping("/customers/{id}")
fun getCustomerById(@PathVariable(value = "id") customerId: Long, @RequestHeader headers: HttpHeaders): ResponseEntity<Customer> { @CrossOrigin(origins = ["http://localhost:48102"])
fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> {
val customer: Customer? = customerRepository.findById(customerId).orElse(null) val customer: Customer? = customerRepository.findById(customerId).orElse(null)
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer) return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
} }

View File

@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import org.springframework.web.bind.annotation.CrossOrigin
@RestController @RestController
@ -42,6 +43,7 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
) { ) {
@GetMapping("/diet_package") @GetMapping("/diet_package")
@CrossOrigin(origins = ["http://localhost:48102"])
fun getDietPackageData(): ResponseEntity<String> { fun getDietPackageData(): ResponseEntity<String> {
val gson = GsonBuilder() val gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation() .excludeFieldsWithoutExposeAnnotation()

View File

@ -28,10 +28,22 @@ class AuthenticationControllerAspect {
Singleton.checkDBUpdate(configurationRepository, properties) Singleton.checkDBUpdate(configurationRepository, properties)
} }
@Before("execution(* com.aitrainer.api.security.JwtSecurityConfig.*(..))") @Before("execution(* com.aitrainer.api.security.JwtSecurityConfig.filterChain(..))")
fun securityControllerAspect(joinPoint: JoinPoint) { fun securityControllerAspect(joinPoint: JoinPoint) {
println("JwtSecurity config join") println("JwtSecurity FilterChain config join")
Singleton.checkDBUpdate(configurationRepository, properties) Singleton.checkDBUpdate(configurationRepository, properties)
} }
@Before("execution(* com.aitrainer.api.security.JwtSecurityConfig.corsMappingConfigurer(..))")
fun corsControllerAspect(joinPoint: JoinPoint) {
println("JwtSecurity CorsMapper config join")
Singleton.checkDBUpdate(configurationRepository, properties)
}
/* @Before("execution(* com.aitrainer.api.security.JwtRequestFilter.doFilterInternal(..))")
fun requestFilterAspect(joinPoint: JoinPoint) {
println("JwtRequestFilter join")
Singleton.checkDBUpdate(configurationRepository, properties)
}*/
} }

View File

@ -12,10 +12,13 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.http.SessionCreationPolicy import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableWebMvc
class JwtSecurityConfig { class JwtSecurityConfig {
@Autowired @Autowired
private val jwtAuthenticationEntryPoint: JwtAuthenticationEntryPoint? = null private val jwtAuthenticationEntryPoint: JwtAuthenticationEntryPoint? = null
@ -33,24 +36,37 @@ class JwtSecurityConfig {
val authenticationManagerBuilder = http.getSharedObject( val authenticationManagerBuilder = http.getSharedObject(
AuthenticationManagerBuilder::class.java AuthenticationManagerBuilder::class.java
) )
authenticationManagerBuilder.userDetailsService(jwtUserDetailsService).passwordEncoder(serviceBeans!!.passwordEncoder()) authenticationManagerBuilder.userDetailsService(jwtUserDetailsService)
.passwordEncoder(serviceBeans!!.passwordEncoder())
return authenticationManagerBuilder.build() return authenticationManagerBuilder.build()
} }
@Bean @Bean
@Throws(Exception::class) @Throws(Exception::class)
fun filterChain(httpSecurity: HttpSecurity):SecurityFilterChain { fun filterChain(httpSecurity: HttpSecurity): SecurityFilterChain {
httpSecurity. httpSecurity.cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("/api/authenticate").permitAll()
csrf().disable(). .anyRequest().authenticated().and().exceptionHandling()
authorizeHttpRequests().requestMatchers("/api/authenticate").permitAll(). .authenticationEntryPoint(jwtAuthenticationEntryPoint).and()
anyRequest().authenticated().and(). .addFilterAfter(jwtRequestFilter, UsernamePasswordAuthenticationFilter::class.java).
exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().
addFilterAfter(jwtRequestFilter, UsernamePasswordAuthenticationFilter::class.java).
// make sure we use stateless session; session won't be used to // make sure we use stateless session; session won't be used to
// store user's state. // store user's state.
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
return httpSecurity.build(); return httpSecurity.build();
} }
}
@Bean
fun corsMappingConfigurer(): WebMvcConfigurer? {
return object : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**")
.allowedOriginPatterns("https://*.diet4you.eu", "https://*.diet4you.hu", "https://*.workouttest.org", "http://localhost:[*]")
//.allowedOrigins("*")
.allowedMethods("POST", "GET", "OPTIONS", "HEAD")
.maxAge(3600)
.allowedHeaders("*")
}
}
}
}

View File

@ -23,3 +23,5 @@ jwt.secret=aitrainer
openai.key=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 openai.key=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
firebase.key=AIzaSyBLn7Bz73Z1hB-OhqphBDsskOyGmpI7J8E firebase.key=AIzaSyBLn7Bz73Z1hB-OhqphBDsskOyGmpI7J8E
spring.mail.properties.mail.mime.charset=UTF-8 spring.mail.properties.mail.mime.charset=UTF-8
spring.mvc.cors.allowed-origins=*

View File

@ -20,4 +20,6 @@ jwt.secret=aitrainer
firebase.key=AIzaSyCUXBWV3_qzvV__ZWZA1siHftrrJpjDKh4 firebase.key=AIzaSyCUXBWV3_qzvV__ZWZA1siHftrrJpjDKh4
openai.key=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 openai.key=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
spring.mail.properties.mail.mime.charset=UTF-8 spring.mail.properties.mail.mime.charset=UTF-8
spring.mvc.cors.allowed-origins=*

View File

@ -23,4 +23,6 @@ jwt.secret=aitrainer
openai.key=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 openai.key=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
spring.mail.properties.mail.mime.charset=UTF-8 spring.mail.properties.mail.mime.charset=UTF-8
firebase.key=AIzaSyCUXBWV3_qzvV__ZWZA1siHftrrJpjDKh4 firebase.key=AIzaSyCUXBWV3_qzvV__ZWZA1siHftrrJpjDKh4
spring.mvc.cors.allowed-origins=*

View File

@ -394,9 +394,9 @@ class AppPackageTest {
val appTextJson: String = record[1] val appTextJson: String = record[1]
val type = object : TypeToken<List<AppText?>?>() {}.type val type = object : TypeToken<List<AppText?>?>() {}.type
val texts: List<AppText> = gson.fromJson(appTextJson, type) val texts: List<AppText> = gson.fromJson(appTextJson, type)
assertEquals(texts.size, 2) assertEquals(texts.size, 15)
assertEquals(texts[0].translations[0].translation, "Done!") assertEquals(texts[13].translations[0].translation, "Done!")
assertEquals(texts[0].translations[1].translation, "Kész!") assertEquals(texts[13].translations[1].translation, "Kész!")
} else if (record[0] == TrainingProgram::class.simpleName) { } else if (record[0] == TrainingProgram::class.simpleName) {
val trainingProgramJson: String = record[1] val trainingProgramJson: String = record[1]
val type = object : TypeToken<List<TrainingProgram?>?>() {}.type val type = object : TypeToken<List<TrainingProgram?>?>() {}.type

View File

@ -58,7 +58,7 @@ class CustomerTests {
val id2: Long = 90 val id2: Long = 90
val controller = CustomerController(customerRepository) val controller = CustomerController(customerRepository)
val response = controller.getCustomerById(id2, HttpHeaders.EMPTY) val response = controller.getCustomerById(id2)
val customer2: Customer = response.body as Customer val customer2: Customer = response.body as Customer
assertNotNull (customer2) assertNotNull (customer2)