Spring REST API Quick Start: Difference between revisions
Jump to navigation
Jump to search
Line 84: | Line 84: | ||
</dependencies> | </dependencies> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Add | ==Add Web Security to App== | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
@SpringBootApplication | @SpringBootApplication | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | public class Restapi11Application extends SpringBootServletInitializer { | ||
public class | |||
public static void main(String[] args) { | |||
SpringApplication.run(Restapi11Application.class, args); | |||
} | |||
} | |||
</syntaxhighlight> | |||
===Amend WebSecurityConfigurerAdapter=== | |||
<syntaxhighlight lang="java" highlight="11"> | |||
@Configuration | |||
@EnableWebSecurity | |||
@EnableGlobalMethodSecurity ( | |||
prePostEnabled = true, | |||
securedEnabled = true, | |||
jsr250Enabled = true | |||
) | |||
public class ConferenceSecurityConfig extends WebSecurityConfigurerAdapter { | |||
@Autowired | |||
private DataSource dataSource; | |||
... | |||
@Override | @Override | ||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception { | protected void configure(final AuthenticationManagerBuilder auth) throws Exception { | ||
// Swap out in memory for jdbc | |||
// auth.inMemoryAuthentication() | |||
//.withUser("iwiseman").password(passwordEncoder().encode("pass")).roles("USER"); | |||
auth.jdbcAuthentication() | |||
.dataSource(dataSource) | |||
.passwordEncoder(passwordEncoder()); | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 05:00, 3 April 2021
Introduction
This is meant to just be a quickstart for me. I use many technologies so this reminds me how to get going.
Setup Spring
Go to start.spring.io and select JPA, Rest Repositories and H2 Database.
Import the resulting project as a maven project
Create Classes
Beer Class (Entity)
@Entity
public class Beer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getApv() {
return apv;
}
public void setApv(Double apv) {
this.apv = apv;
}
private String name;
private Double apv;
}
Beer Repository
package nz.co.bibble.restapi11;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface RestRepository extends CrudRepository<Beer,Long> {
}
Resources
Set the application properties for H2
spring.datasource.url=jdbc:h2:mem:beers
Create the Data.sql in the resources directory
INSERT INTO beer(name, apv) VALUES('Jai Alai', 7.5);
INSERT INTO beer(name, apv) VALUES('Stella Artois', 5.0);
INSERT INTO beer(name, apv) VALUES('Lagunitas', 6.2);
COMMIT;
Run
Goto http://localhost:8080/beers
Add Spring Security
Add Dependencies
We can add security to a new project with
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Add Web Security to App
@SpringBootApplication
public class Restapi11Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Restapi11Application.class, args);
}
}
Amend WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity (
prePostEnabled = true,
securedEnabled = true,
jsr250Enabled = true
)
public class ConferenceSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
...
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
// Swap out in memory for jdbc
// auth.inMemoryAuthentication()
//.withUser("iwiseman").password(passwordEncoder().encode("pass")).roles("USER");
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder());