Skip to content

Commit e07eea1

Browse files
committed
refactorings for error handling and security fixes
1 parent 3441d56 commit e07eea1

File tree

6 files changed

+41
-55
lines changed

6 files changed

+41
-55
lines changed

src/main/java/com/reljicd/DemoApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@SpringBootApplication
77
public class DemoApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(DemoApplication.class, args);
11-
}
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
1212
}

src/main/java/com/reljicd/controller/exception/ErrorController.java renamed to src/main/java/com/reljicd/config/GlobalExceptionHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reljicd.controller.exception;
1+
package com.reljicd.config;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
@@ -15,16 +15,16 @@
1515
* @author Dusan
1616
*/
1717
@ControllerAdvice
18-
public class ErrorController {
18+
public class GlobalExceptionHandler {
1919

20-
private static Logger logger = LoggerFactory.getLogger(ErrorController.class);
20+
private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
2121

2222
@ExceptionHandler(Throwable.class)
2323
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
2424
public ModelAndView exception(final Throwable throwable, final Model model) {
2525
logger.error("Exception during execution of SpringSecurity application", throwable);
2626

27-
ModelAndView modelAndView = new ModelAndView("error");
27+
ModelAndView modelAndView = new ModelAndView("/error");
2828
String errorMessage = (throwable != null ? throwable.toString() : "Unknown error");
2929
modelAndView.addObject("errorMessage", errorMessage);
3030
return modelAndView;

src/main/java/com/reljicd/error/MyAccessDeniedHandler.java renamed to src/main/java/com/reljicd/config/MyAccessDeniedHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reljicd.error;
1+
package com.reljicd.config;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
@@ -30,9 +30,7 @@ public void handle(HttpServletRequest httpServletRequest,
3030
= SecurityContextHolder.getContext().getAuthentication();
3131

3232
if (auth != null) {
33-
logger.info("User '" + auth.getName()
34-
+ "' attempted to access the protected URL: "
35-
+ httpServletRequest.getRequestURI());
33+
logger.info(String.format("User '%s' attempted to access the protected URL: %s", auth.getName(), httpServletRequest.getRequestURI()));
3634
}
3735

3836
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/403");

src/main/java/com/reljicd/config/SpringSecurityConfig.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*
2121
* @author Dusan
2222
*/
23-
//@EnableWebSecurity
2423
@Configuration
2524
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
2625

@@ -44,9 +43,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
4443

4544
/**
4645
* HTTPSecurity configurer
47-
* - roles ADMIN allow to access /admin/**
48-
* - roles USER allow to access /user/** and /newPost/**
49-
* - anybody can visit /, /home, /about, /registration, /error, /blog/**, /post/**, /h2-console/**
46+
* - roles USER allow to access to /link/** and /newLink/**
47+
* - anybody can visit /home, /registration, /error, /links/**, /post/**, /h2-console/**
5048
* - every other page needs authentication
5149
* - custom 403 access denied handler
5250
*
@@ -58,11 +56,8 @@ protected void configure(HttpSecurity http) throws Exception {
5856

5957
http.csrf().disable()
6058
.authorizeRequests()
61-
.antMatchers("/", "/home", "/about", "/registration", "/error", "/blog/**", "/post/**", "/h2-console/**").permitAll()
62-
.antMatchers("/admin/**").hasAnyRole("ADMIN")
63-
.antMatchers("/user/**", "/newPost/**").hasAnyRole("USER")
64-
// permit all to see HAL Browser
65-
// .antMatchers("/browser/**").permitAll()
59+
.antMatchers("/home", "/registration", "/error", "/links/**", "/post/**", "/h2-console/**").permitAll()
60+
.antMatchers("link/**", "/newLink/**").hasAnyRole("USER")
6661
.anyRequest().authenticated()
6762
.and()
6863
.formLogin()
@@ -81,9 +76,6 @@ protected void configure(HttpSecurity http) throws Exception {
8176

8277
/**
8378
* Authentication details
84-
*
85-
* @param auth
86-
* @throws Exception
8779
*/
8880
@Autowired
8981
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
@@ -98,15 +90,11 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
9890

9991
// In memory authentication
10092
auth.inMemoryAuthentication()
101-
// .withUser("user").password("password").roles("USER")
102-
// .and()
10393
.withUser(adminUsername).password(adminPassword).roles("ADMIN");
10494
}
10595

10696
/**
10797
* Configure and return BCrypt password encoder
108-
*
109-
* @return
11098
*/
11199
@Bean
112100
public PasswordEncoder passwordEncoder() {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.reljicd.controller;
2+
3+
import org.springframework.boot.autoconfigure.web.ErrorController;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.servlet.ModelAndView;
8+
9+
@RestController
10+
public class CustomErrorController implements ErrorController {
11+
12+
private static final String PATH = "/error";
13+
14+
@RequestMapping(PATH)
15+
public ModelAndView error() {
16+
return new ModelAndView("/error");
17+
}
18+
19+
@GetMapping("/403")
20+
public ModelAndView error403() {
21+
return new ModelAndView("/403");
22+
}
23+
24+
@Override
25+
public String getErrorPath() {
26+
return PATH;
27+
}
28+
}

src/main/java/com/reljicd/controller/DefaultController.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy