Our Best Selling Spring Boot Courses
More Courses and Videos From in28Minutes
<?xml version =" 1.0" encoding =" UTF-8" ?>
<project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion >4.0.0</modelVersion >
<groupId >com.in28minutes.springboot</groupId >
<artifactId >student-services-security</artifactId >
<version >0.0.1-SNAPSHOT</version >
<packaging >jar</packaging >
<name >student-services</name >
<description >Demo project for Spring Boot</description >
<parent >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-parent</artifactId >
<version >2.0.0.RELEASE</version >
<relativePath /> <!-- lookup parent from repository -->
</parent >
<properties >
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
<project .reporting.outputEncoding>UTF-8</project .reporting.outputEncoding>
<java .version>1.8</java .version>
</properties >
<dependencies >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-actuator</artifactId >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-web</artifactId >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-security</artifactId >
</dependency >
<dependency >
<groupId >org.apache.tomcat.embed</groupId >
<artifactId >tomcat-embed-jasper</artifactId >
<scope >provided</scope >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-devtools</artifactId >
<scope >runtime</scope >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-test</artifactId >
<scope >test</scope >
</dependency >
</dependencies >
<build >
<plugins >
<plugin >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-maven-plugin</artifactId >
</plugin >
</plugins >
</build >
<repositories >
<repository >
<id >spring-snapshots</id >
<name >Spring Snapshots</name >
<url >https://repo.spring.io/snapshot</url >
<snapshots >
<enabled >true</enabled >
</snapshots >
</repository >
<repository >
<id >spring-milestones</id >
<name >Spring Milestones</name >
<url >https://repo.spring.io/milestone</url >
<snapshots >
<enabled >false</enabled >
</snapshots >
</repository >
</repositories >
<pluginRepositories >
<pluginRepository >
<id >spring-snapshots</id >
<name >Spring Snapshots</name >
<url >https://repo.spring.io/snapshot</url >
<snapshots >
<enabled >true</enabled >
</snapshots >
</pluginRepository >
<pluginRepository >
<id >spring-milestones</id >
<name >Spring Milestones</name >
<url >https://repo.spring.io/milestone</url >
<snapshots >
<enabled >false</enabled >
</snapshots >
</pluginRepository >
</pluginRepositories >
</project >
/src/main/java/com/in28minutes/springboot/controller/LoginController.java
package com .in28minutes .springboot .controller ;
import org .springframework .stereotype .Controller ;
import org .springframework .ui .ModelMap ;
import org .springframework .web .bind .annotation .RequestMapping ;
import org .springframework .web .bind .annotation .RequestMethod ;
@ Controller
public class LoginController {
@ RequestMapping (value = "/" , method = RequestMethod .GET )
public String showLoginPage (ModelMap model ) {
model .put ("name" , "in28Minutes" );
return "welcome" ;
}
}
/src/main/java/com/in28minutes/springboot/controller/StudentController.java
package com .in28minutes .springboot .controller ;
import java .util .List ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .web .bind .annotation .GetMapping ;
import org .springframework .web .bind .annotation .PathVariable ;
import org .springframework .web .bind .annotation .RestController ;
import com .in28minutes .springboot .model .Course ;
import com .in28minutes .springboot .service .StudentService ;
@ RestController
public class StudentController {
@ Autowired
private StudentService studentService ;
@ GetMapping ("/students/{studentId}/courses" )
public List <Course > retrieveCoursesForStudent (@ PathVariable String studentId ) {
return studentService .retrieveCourses (studentId );
}
}
/src/main/java/com/in28minutes/springboot/model/Course.java
package com .in28minutes .springboot .model ;
import java .util .List ;
public class Course {
private String id ;
private String name ;
private String description ;
private List <String > steps ;
// Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException:
// Can not construct instance of com.in28minutes.springboot.model.Course:
// no suitable constructor found, can not deserialize from Object value
// (missing default constructor or creator, or perhaps need to add/enable
// type information?)
public Course () {
}
public Course (String id , String name , String description , List <String > steps ) {
super ();
this .id = id ;
this .name = name ;
this .description = description ;
this .steps = steps ;
}
public String getId () {
return id ;
}
public void setId (String id ) {
this .id = id ;
}
public String getDescription () {
return description ;
}
public String getName () {
return name ;
}
public List <String > getSteps () {
return steps ;
}
@ Override
public String toString () {
return String .format (
"Course [id=%s, name=%s, description=%s, steps=%s]" , id , name ,
description , steps );
}
@ Override
public int hashCode () {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ((id == null ) ? 0 : id .hashCode ());
return result ;
}
@ Override
public boolean equals (Object obj ) {
if (this == obj )
return true ;
if (obj == null )
return false ;
if (getClass () != obj .getClass ())
return false ;
Course other = (Course ) obj ;
if (id == null ) {
if (other .id != null )
return false ;
} else if (!id .equals (other .id ))
return false ;
return true ;
}
}
/src/main/java/com/in28minutes/springboot/model/Student.java
package com .in28minutes .springboot .model ;
import java .util .List ;
public class Student {
private String id ;
private String name ;
private String description ;
private List <Course > courses ;
public Student (String id , String name , String description ,
List <Course > courses ) {
super ();
this .id = id ;
this .name = name ;
this .description = description ;
this .courses = courses ;
}
public String getId () {
return id ;
}
public void setId (String id ) {
this .id = id ;
}
public String getName () {
return name ;
}
public void setName (String name ) {
this .name = name ;
}
public String getDescription () {
return description ;
}
public void setDescription (String description ) {
this .description = description ;
}
public List <Course > getCourses () {
return courses ;
}
public void setCourses (List <Course > courses ) {
this .courses = courses ;
}
@ Override
public String toString () {
return String .format (
"Student [id=%s, name=%s, description=%s, courses=%s]" , id ,
name , description , courses );
}
}
/src/main/java/com/in28minutes/springboot/security/SecurityConfig.java
package com .in28minutes .springboot .security ;
import org .springframework .context .annotation .Configuration ;
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
@ Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// Authentication : User --> Roles
protected void configure (AuthenticationManagerBuilder auth )
throws Exception {
auth .inMemoryAuthentication ().passwordEncoder (org .springframework .security .crypto .password .NoOpPasswordEncoder .getInstance ()).withUser ("user1" ).password ("secret1" )
.roles ("USER" ).and ().withUser ("admin1" ).password ("secret1" )
.roles ("USER" , "ADMIN" );
}
// Authorization : Role -> Access
protected void configure (HttpSecurity http ) throws Exception {
http .httpBasic ().and ().authorizeRequests ().antMatchers ("/students/**" )
.hasRole ("USER" ).antMatchers ("/**" ).hasRole ("ADMIN" ).and ()
.csrf ().disable ().headers ().frameOptions ().disable ();
}
}
/src/main/java/com/in28minutes/springboot/service/StudentService.java
package com .in28minutes .springboot .service ;
import java .util .ArrayList ;
import java .util .Arrays ;
import java .util .List ;
import org .springframework .stereotype .Component ;
import com .in28minutes .springboot .model .Course ;
import com .in28minutes .springboot .model .Student ;
@ Component
public class StudentService {
private static List <Student > students = new ArrayList <>();
static {
// Initialize Data
Course course1 = new Course ("Course1" , "Spring" , "10 Steps" ,
Arrays .asList ("Learn Maven" , "Import Project" , "First Example" ,
"Second Example" ));
Course course2 = new Course ("Course2" , "Spring MVC" , "10 Examples" ,
Arrays .asList ("Learn Maven" , "Import Project" , "First Example" ,
"Second Example" ));
Student ranga = new Student ("Student1" , "Ranga Karanam" ,
"Hiker, Programmer and Architect" , new ArrayList <>(
Arrays .asList (course1 , course2 )));
students .add (ranga );
}
public Student retrieveStudent (String studentId ) {
for (Student student : students ) {
if (student .getId ().equals (studentId )) {
return student ;
}
}
return null ;
}
public List <Course > retrieveCourses (String studentId ) {
Student student = retrieveStudent (studentId );
if (student == null ) {
return null ;
}
return student .getCourses ();
}
}
/src/main/java/com/in28minutes/springboot/StudentServicesApplication.java
package com .in28minutes .springboot ;
import org .springframework .boot .SpringApplication ;
import org .springframework .boot .autoconfigure .SpringBootApplication ;
@ SpringBootApplication
public class StudentServicesApplication {
public static void main (String [] args ) {
SpringApplication .run (StudentServicesApplication .class , args );
}
}
/src/main/resources/application.properties
spring.mvc.view.prefix =/WEB-INF/jsp/
spring.mvc.view.suffix =.jsp
/src/main/webapp/WEB-INF/jsp/welcome.jsp
<div class="container">
Welcome ${name}!!
</div>
/src/test/java/com/in28minutes/springboot/StudentServicesApplicationTests.java
package com .in28minutes .springboot ;
import org .junit .Test ;
import org .junit .runner .RunWith ;
import org .springframework .boot .test .context .SpringBootTest ;
import org .springframework .test .context .junit4 .SpringRunner ;
@ RunWith (SpringRunner .class )
@ SpringBootTest
public class StudentServicesApplicationTests {
@ Test
public void contextLoads () {
}
}