Skip to content

Instantly share code, notes, and snippets.

View Tonel's full-sized avatar

Antonello Zanini Tonel

View GitHub Profile
@Component
class AuthTokenHandler {
@Autowired
lateinit var authTokenDao: AuthTokenDao
@Transactional(readOnly = true)
fun getUserFromToken(token : String?) : User {
if (token == null)
throw AuthenticationException()
class AuthTokenWebResolver : HandlerMethodArgumentResolver {
@Autowired
lateinit var authTokenHandler: AuthTokenHandler
// to register Auth annotation
override fun supportsParameter(methodParameter: MethodParameter): Boolean {
return methodParameter.getParameterAnnotation(Auth::class.java) != null
}
@Configuration
class CustomConfig : WebMvcConfigurationSupport() {
@Bean
fun authWebArgumentResolverFactory() : HandlerMethodArgumentResolver {
return AuthWebResolver()
}
// Addding the AuthWebResolver to the default argument resolvers
override fun addArgumentResolvers(argumentResolvers: MutableList<HandlerMethodArgumentResolver>) {
@GetMapping("data/{id}")
fun getData(
@Auth user : User,
@PathVariable(value = "id") id: Int
) : ResponseEntity<Void> {
// NOTE: user can be used inside the method body
// API logic
@GetMapping("data/{id}")
fun getData(
@PathVariable(value = "id") id: Int
) : ResponseEntity<Void> {
// API logic
return ResponseEntity(HttpStatus.OK)
}
@ControllerAdvice
class CustomExceptionsHandler {
@ExceptionHandler(AuthenticationException::class)
fun authenticationExceptionHandler(e: Exception): ResponseEntity<String> {
return ResponseEntity("authToken missing or not valid!", HttpStatus.UNAUTHORIZED)
}
}
import axios from 'axios';
// sample configurations
const axiosInstance = axios.create({
withCredentials: true,
baseURL: 'https://medium.org'
});
export default axiosInstance;
import axiosInstance from './axiosConfig'
export const Author = {
getAll: function() {
return axiosInstance.request({
method: "GET",
url: `/api/v1/authors`
});
},
getById: function(authorId) {
import { Author as AuthorAPI } from './Author'
import { Book as BookAPI } from './Book'
export {
AuthorAPI,
BookAPI
}
import React, {useEffect, useState} from "react";
import {AuthorAPI} from "../../api/index";
function AuthorCard(props) {
const { authorId } = props;
const [author, setAuthor] = useState(undefined)
useEffect(() => {
AuthorAPI.getById(authorId).then(