Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
administration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OZG-Cloud
app
administration
Commits
08eebf49
Commit
08eebf49
authored
1 year ago
by
OZGCloud
Browse files
Options
Downloads
Patches
Plain Diff
OZG-4939 Unauthorized error response
parent
4df77278
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/de/ozgcloud/admin/security/SecurityConfiguration.java
+38
-7
38 additions, 7 deletions
...ava/de/ozgcloud/admin/security/SecurityConfiguration.java
with
38 additions
and
7 deletions
src/main/java/de/ozgcloud/admin/security/SecurityConfiguration.java
+
38
−
7
View file @
08eebf49
...
...
@@ -19,9 +19,14 @@
*/
package
de.ozgcloud.admin.security
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.net.URI
;
import
java.util.List
;
import
org.apache.http.HttpHeaders
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpMethod
;
...
...
@@ -32,9 +37,15 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
;
import
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
;
import
org.springframework.security.config.http.SessionCreationPolicy
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.oauth2.core.oidc.StandardClaimNames
;
import
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter
;
import
org.springframework.security.web.SecurityFilterChain
;
import
org.springframework.web.ErrorResponse
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectWriter
;
@Configuration
@EnableMethodSecurity
(
securedEnabled
=
true
)
...
...
@@ -52,12 +63,7 @@ public class SecurityConfiguration {
// Disable CSRF because of state-less session-management
http
.
csrf
(
AbstractHttpConfigurer:
:
disable
);
// Return 401 (unauthorized) instead of 302 (redirect to login) when
// authorization is missing or invalid
http
.
exceptionHandling
(
eh
->
eh
.
authenticationEntryPoint
((
request
,
response
,
authException
)
->
{
response
.
addHeader
(
HttpHeaders
.
WWW_AUTHENTICATE
,
"Bearer realm=\"Restricted Content\""
);
response
.
sendError
(
HttpStatus
.
UNAUTHORIZED
.
value
(),
HttpStatus
.
UNAUTHORIZED
.
getReasonPhrase
());
}));
http
.
exceptionHandling
(
eh
->
eh
.
authenticationEntryPoint
(
this
::
handleAuthenticationException
));
http
.
authorizeHttpRequests
(
requests
->
requests
.
requestMatchers
(
HttpMethod
.
GET
,
"/api/environment"
).
permitAll
()
...
...
@@ -71,6 +77,31 @@ public class SecurityConfiguration {
return
http
.
build
();
}
private
void
handleAuthenticationException
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AuthenticationException
authException
)
throws
IOException
{
response
.
setStatus
(
HttpStatus
.
UNAUTHORIZED
.
value
());
response
.
setContentType
(
"application/json"
);
var
problemDetail
=
getProblemDetailAsString
(
request
.
getRequestURI
(),
authException
);
writeProblemDetailToBody
(
response
.
getWriter
(),
problemDetail
);
}
private
String
getProblemDetailAsString
(
String
requestUri
,
AuthenticationException
authException
)
throws
JsonProcessingException
{
var
problemDetail
=
ErrorResponse
.
builder
(
authException
,
HttpStatus
.
UNAUTHORIZED
,
authException
.
getLocalizedMessage
()).
build
().
getBody
();
problemDetail
.
setInstance
(
URI
.
create
(
requestUri
));
ObjectWriter
ow
=
new
ObjectMapper
().
writer
().
withDefaultPrettyPrinter
();
return
ow
.
writeValueAsString
(
problemDetail
);
}
private
void
writeProblemDetailToBody
(
PrintWriter
writer
,
String
problemDetail
)
{
writer
.
print
(
problemDetail
);
writer
.
flush
();
writer
.
close
();
}
@Bean
JwtAuthenticationConverter
jwtAuthenticationConverter
()
{
var
jwtConverter
=
new
JwtAuthenticationConverter
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment