Last active
July 4, 2024 23:16
-
-
Save rajeshhazari/a5fb09bb2f7d8ec31924268e7976bcd9 to your computer and use it in GitHub Desktop.
spring boot rest controller sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/api") | |
public class ZipController { | |
@PostMapping("/download-zip") | |
public ResponseEntity<ByteArrayResource> downloadZip() throws IOException { | |
// Create a ByteArrayOutputStream to hold the ZIP content | |
String fileName = UUID.randomUUID().toString() + ".zip"; | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
try (ZipOutputStream zipOut = new ZipOutputStream(baos)) { | |
// Add a README.md file to the ZIP | |
addToZip(zipOut, "readme.md", "This is a sample README file."); | |
// You can add more files if needed | |
// Finish writing to the ZIP | |
zipOut.finish(); | |
} | |
// Set up HTTP headers | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); | |
headers.setContentDispositionFormData("attachment", fileName); | |
// Create a ByteArrayResource from the ZIP content | |
ByteArrayResource resource = new ByteArrayResource(baos.toByteArray()); | |
// Return the ZIP file as a ByteArrayResource | |
return ResponseEntity.ok() | |
.headers(headers) | |
.body(resource); | |
} | |
private void addToZip(ZipOutputStream zipOut, String fileName, String content) throws IOException { | |
// Create a new ZIP entry | |
zipOut.putNextEntry(new ZipEntry(fileName)); | |
// Write content to the ZIP entry | |
zipOut.write(content.getBytes()); | |
// Close the ZIP entry | |
zipOut.closeEntry(); | |
} | |
} | |
@Configuration | |
@EnableWebSecurity() | |
static class SecurityConfig { | |
public static class CustomAuthenticationProvider implements AuthenticationProvider { | |
private final String username; | |
private final String password; | |
private final String role; | |
CustomAuthenticationProvider(String username, String password, String role) { | |
this.username = username; | |
this.password = password; | |
this.role = role; | |
} | |
@Override | |
public Authentication authenticate(Authentication auth) | |
throws AuthenticationException { | |
String u = auth.getName(); | |
String p = auth.getCredentials().toString(); | |
if (username.equals(u) && password.equals(p)) { | |
return new UsernamePasswordAuthenticationToken | |
(username, password, Collections.singletonList(new SimpleGrantedAuthority(role))); | |
} else { | |
throw new | |
BadCredentialsException("External system authentication failed"); | |
} | |
} | |
@Override | |
public boolean supports(Class<?> auth) { | |
return auth.equals(UsernamePasswordAuthenticationToken.class); | |
} | |
} | |
@Bean | |
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { | |
AuthenticationManagerBuilder authenticationManagerBuilder = | |
httpSecurity.getSharedObject(AuthenticationManagerBuilder.class); | |
authenticationManagerBuilder.authenticationProvider(new CustomAuthenticationProvider("user", "password", "USER")); | |
authenticationManagerBuilder.authenticationProvider(new CustomAuthenticationProvider("user1", "password", "USER")); | |
authenticationManagerBuilder.authenticationProvider(new CustomAuthenticationProvider("admin", "password", "ADMIN")); | |
authenticationManagerBuilder.authenticationProvider(new CustomAuthenticationProvider("admin1", "password", "ADMIN")); | |
httpSecurity.authenticationManager(authenticationManagerBuilder.build()); | |
httpSecurity | |
.authorizeHttpRequests(auth -> { | |
auth.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll(); | |
auth.anyRequest().fullyAuthenticated(); | |
}); | |
httpSecurity.logout(new LogoutCustomizer()); | |
httpSecurity.formLogin(withDefaults()); | |
return httpSecurity.build(); | |
} | |
@Bean | |
public PasswordEncoder passwordEncoder() { | |
return NoOpPasswordEncoder.getInstance(); | |
} | |
} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Secured</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Secured</h1> | |
<form th:action="@{/logout}" method="post"> | |
<input id="logout" type="submit" value="Log Out"/> | |
</form> | |
<form id="downloadZip" th:action="@{/api/download-zip}" method="post"> | |
<input id="download" type="submit" value="Download"/> | |
</form> | |
</div> | |
</body> | |
</html> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>3.2.0</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<java.version>17</java.version> | |
<testcontainers.version>1.18.3</testcontainers.version> | |
<jsonwebtoken.version>0.11.5</jsonwebtoken.version> | |
<lombok.version>1.18.28</lombok.version> | |
<springdoc.webstarter.ui.version>2.1.0</springdoc.webstarter.ui.version> | |
<springdoc.openapi.ui.version>1.7.0</springdoc.openapi.ui.version> | |
<kafka.clients.version>3.4.0</kafka.clients.version> | |
<jacoco-plugin.version>0.8.8</jacoco-plugin.version> | |
<sonar.jacoco.itReportPath>${project.build.outputDirectory}/coverage/jacoco/jacoco-it.exec</sonar.jacoco.itReportPath> | |
<sonar.jacoco.reportPath>${project.build.outputDirectory}/coverage/jacoco/jacoco.exec</sonar.jacoco.reportPath> | |
<sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin> | |
<sonar-maven-plugin.version>3.2</sonar-maven-plugin.version> | |
<start-class>example.WebfluxFormApplication</start-class> | |
<openapi-generator-maven-plugin.version>6.2.0</openapi-generator-maven-plugin.version> | |
</properties> | |
<dependencies> | |
<!-- Spring Boot Starters --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-webflux</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-thymeleaf</artifactId> | |
<version>3.2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.thymeleaf.extras</groupId> | |
<artifactId>thymeleaf-extras-springsecurity6</artifactId> | |
</dependency> | |
<!-- Spring Boot Test Dependencies --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<!-- Integration Test Dependencies --> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>htmlunit-driver</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Ikhiloya/d1d0d034c63199eed2c67a9a1a400152