1
1
.
.
4
4
.
.
2
2
S
S
e
e
c
c
u
u
r
r
i
i
t
t
y
y
E
E
x
x
p
p
r
r
e
e
s
s
s
s
i
i
o
o
n
n
s
s
-
-
@
@
S
S
e
e
c
c
u
u
r
r
e
e
d
d
-
-
R
R
o
o
l
l
e
e
s
s
I
I
n
n
f
f
o
o
[
[
G
G
]
]
When you add Spring Boot Starter Security every URL becomes unavailable unless you Sign In.
This tutorial shows how to loosen up this restriction by allowing access to certain URLs without having to Sign In.
We will do this by creating a Class SecurityConfig which extends and Overrides Method configure().
@Secured can only be used with Roles.
SecurityConfig.java
@EnableGlobalMethodSecurity(securedEnabled = true)
MyController.java
@Secured("ROLE_ADMIN")
@Secured({"ROLE_ADMIN", "ROLE_USER"})
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat Server
Security
Spring Security
Enables Spring Security
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_security_expression_secured (add Spring Boot Starters from the table)
Edit File: application.properties (specify Username, Password, Role)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Package: config (inside main package)
– Create Class: SecurityConfig.java (inside config package)
application.properties
# SECURITY
spring.security.user.name = myuser
spring.security.user.password = mypassword
spring.security.user.roles = USER, LOADER
MyController
http://localhost:8080/endPoint1
Tomcat
Browser
SecurityConfig
SecurityConfig.java
package com.ivoronline.springboot_security_expression_secured.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
//SPECIFY ACCESS TO ENDPOINTS
httpSecurity.authorizeRequests()
.antMatchers("/endPoint1").denyAll() //No access (even after log in)
.antMatchers("/endPoint2").permitAll(); //No log in (anonymous access)
//REDIRECT TO LOGIN FORM
httpSecurity.formLogin();
}
}
MyController.java
package com.ivoronline.springboot_security_expression_secured.controllers;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/endPoint1")
public String ep1() { return "endPoint1"; }
@ResponseBody
@RequestMapping("/endPoint2")
public String ep2() { return "endPoint2"; }
@Secured("ROLE_ADMIN")
@ResponseBody
@RequestMapping("/endPoint3")
public String ep3() { return "endPoint3"; }
@Secured({"ROLE_ADMIN", "ROLE_USER"})
@ResponseBody
@RequestMapping("/endPoint4")
public String ep4() { return "endPoint4"; }
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/endPoint1 denyAll() => deny ALL after Login
http://localhost:8080/endPoint2 permitAll() => allow ALL without Login
http://localhost:8080/endPoint3 hasRole("ADMIN") => deny USER after Login
http://localhost:8080/endPoint4 hasAnyRole("ADMIN", "USER") => allow USER after Login