{"id":22191657,"url":"https://github.com/leosimoes/java-spring-in-memory-authenticator","last_synced_at":"2025-07-22T13:04:39.685Z","repository":{"id":230611119,"uuid":"779781255","full_name":"leosimoes/Java-Spring-In-Memory-Authenticator","owner":"leosimoes","description":"Java project with Spring and Gradle for basic in-memory authentication with authorization for routes.","archived":false,"fork":false,"pushed_at":"2024-04-04T17:45:58.000Z","size":447,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T20:43:38.791Z","etag":null,"topics":["authentication","autorization","java","spring"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leosimoes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-30T19:13:40.000Z","updated_at":"2024-03-30T22:04:01.000Z","dependencies_parsed_at":"2024-04-04T18:48:52.782Z","dependency_job_id":"04e03ee4-802d-4a69-bc28-71499e516916","html_url":"https://github.com/leosimoes/Java-Spring-In-Memory-Authenticator","commit_stats":null,"previous_names":["leosimoes/java-spring-in-memory-authenticator"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leosimoes/Java-Spring-In-Memory-Authenticator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FJava-Spring-In-Memory-Authenticator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FJava-Spring-In-Memory-Authenticator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FJava-Spring-In-Memory-Authenticator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FJava-Spring-In-Memory-Authenticator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leosimoes","download_url":"https://codeload.github.com/leosimoes/Java-Spring-In-Memory-Authenticator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FJava-Spring-In-Memory-Authenticator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266499015,"owners_count":23938771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","autorization","java","spring"],"created_at":"2024-12-02T12:17:07.857Z","updated_at":"2025-07-22T13:04:39.659Z","avatar_url":"https://github.com/leosimoes.png","language":"Java","readme":"# Spring Security - In-memory Authenticator\nJava project with Spring and Gradle for basic in-memory authentication with authorization for routes.\n\nUML Class Diagram:\n\n![Image-04-InMemoryAuthenticator](images/Img-04-UML-Class-InMemoryAuthenticator.png)\n\nRoutes:\n- `/`\n- `/users`\n- `/admins`\n- `/accessDenied`\n\n\n## Steps\nThe steps of project implementation:\n\n1. Create project (in IntelliJ) with:\n- Java language (17);\n- Spring Framework (6.2.3);\n- Dependencies: Web and Security.\n\n![Image-01-IntelliJ](images/Img-01-IntelliJ.png)\n\n2. Create the `RoutesController` class:\n- in the `controllers` package;\n- with the annotation `@RestController`;\n- with the routes `/`, `/users`, `/admins`, `/accessDenied` of type GET.\n\n![Image-02-RoutesController](images/Img-02-UML-Class-RoutesController.png)\n\n3. Create the `SecurityConfig` class:\n- in the `security` package;\n- with the annotations `@Configuration` and `@EnableWebSecurity`;\n- with all methods annotated with `@Bean`;\n- with the following public methods:\n    - `SecurityFilterChain securityFilterChain(HttpSecurity http)` to configure authorization for each route;\n    - `UserDetailsService userDetailsService()` to create users;\n    - `PasswordEncoder passwordEncoder()` to return an instance of `BCryptPasswordEncoder`;\n    - `AuthenticationManager authenticationManager(UserDetailsService UserDetailsService,\n      PasswordEncoder passwordEncoder)` to customize the authenticator with passwordEncoder;\n\n![Image-03-SecurityConfig](images/Img-03-UML-Class-SecurityConfig.png)\n\n\n## Code\n\n```java\n@RestController\npublic class RoutesController {\n\n    @GetMapping(\"/\")\n    public String home(){\n        return \"Home Page - Allowed for everyone\";\n    }\n\n    @GetMapping(\"/users\")\n    public String users(){\n        return \"Users Page - Allowed for logged-in users and administrators\";\n    }\n\n    @GetMapping(\"/admins\")\n    public String admins(){\n        return \"Admins Page - Allowed for logged-in admins\";\n    }\n\n    @GetMapping(\"/accessDenied\")\n    public String accessDenied(){\n        return \"Access denied Page\";\n    }\n\n}\n```\n\n\n```java\n@Configuration\n@EnableWebSecurity\npublic class SecurityConfig {\n\n    @Bean\n    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {\n        http\n                .authorizeHttpRequests((authorize) -\u003e authorize\n                        .requestMatchers(\"/\").permitAll()\n                        .requestMatchers(\"/users\").hasAnyRole(\"USER\", \"ADMIN\")\n                        .requestMatchers(\"/admins\").hasRole(\"ADMIN\")\n                        .anyRequest().authenticated())\n                .exceptionHandling(ex -\u003e ex.accessDeniedPage(\"/accessDenied\"))\n                .httpBasic(Customizer.withDefaults())\n                .formLogin(AbstractHttpConfigurer::disable)\n                .logout(AbstractHttpConfigurer::disable)\n                .sessionManagement(session -\u003e session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)\n                );\n\n        return http.build();\n    }\n\n    @Bean\n    public UserDetailsService userDetailsService() {\n        UserDetails user = User\n                .withDefaultPasswordEncoder()\n                .username(\"usuario\")\n                .password(\"senha\")\n                .roles(\"USER\")\n                .build();\n\n        UserDetails admin = User\n                .withDefaultPasswordEncoder()\n                .username(\"administrador\")\n                .password(\"codigo\")\n                .roles(\"ADMIN\")\n                .build();\n\n        return new InMemoryUserDetailsManager(user, admin);\n    }\n\n    @Bean\n    public PasswordEncoder passwordEncoder() {\n        return PasswordEncoderFactories.createDelegatingPasswordEncoder();\n    }\n\n    @Bean\n    public AuthenticationManager authenticationManager(UserDetailsService UserDetailsService,\n                                                       PasswordEncoder passwordEncoder) {\n        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();\n        daoAuthenticationProvider.setUserDetailsService(UserDetailsService);\n        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);\n\n        return new ProviderManager(daoAuthenticationProvider);\n    }\n}\n```\n\n\n## References\nhttps://docs.spring.io/spring-security/reference/servlet/authentication/passwords/index.html","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleosimoes%2Fjava-spring-in-memory-authenticator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleosimoes%2Fjava-spring-in-memory-authenticator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleosimoes%2Fjava-spring-in-memory-authenticator/lists"}