lista kursów

This commit is contained in:
unknown 2020-05-02 13:02:06 +02:00
parent 1d5744c16e
commit 2f164e7922
7 changed files with 43 additions and 30 deletions

View File

@ -8,7 +8,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class MvcConfig implements WebMvcConfigurer { public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) { public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/courses").setViewName("addCourse"); registry.addViewController("/courses/addCourse").setViewName("addCourse");
registry.addViewController("/").setViewName("start"); registry.addViewController("/").setViewName("start");
registry.addViewController("/login").setViewName("login"); registry.addViewController("/login").setViewName("login");
registry.addViewController("/start").setViewName("start"); registry.addViewController("/start").setViewName("start");

View File

@ -36,6 +36,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() http.authorizeRequests()
.antMatchers( "/", "/courses/**").authenticated() .antMatchers( "/", "/courses/**").authenticated()
.antMatchers( "/courses").authenticated()
.antMatchers("/", "/register").permitAll() .antMatchers("/", "/register").permitAll()
.antMatchers("/", "/start").authenticated() .antMatchers("/", "/start").authenticated()
//.anyRequest().authenticated() //.anyRequest().authenticated()

View File

@ -12,18 +12,20 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@Controller @Controller
@RequestMapping("/courses")
public class CourseController { public class CourseController {
@Autowired @Autowired
private CourseRepository courseRepository; private CourseRepository courseRepository;
@GetMapping("/courses") @GetMapping("")
public String listCourses() { public String listCourses() {
//model.addAttribute("message", name); //model.addAttribute("message", name);

View File

@ -1,9 +1,12 @@
package com.wmi.lti.controllers; package com.wmi.lti.controllers;
import com.wmi.lti.model.course.Course;
import com.wmi.lti.model.course.CourseRepository;
import com.wmi.lti.model.user.UserPrincipal; import com.wmi.lti.model.user.UserPrincipal;
import org.imsglobal.lti.launch.LtiOauthSigner; import org.imsglobal.lti.launch.LtiOauthSigner;
import org.imsglobal.lti.launch.LtiSigner; import org.imsglobal.lti.launch.LtiSigner;
import org.imsglobal.lti.launch.LtiSigningException; import org.imsglobal.lti.launch.LtiSigningException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -11,39 +14,39 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Controller @Controller
public class StartController { public class StartController {
@PostMapping("/start") @Autowired
public String start( private CourseRepository courseRepository;
@RequestParam(name = "key", required = false, defaultValue = "")
String key, @RequestParam(name = "secret", required = false, defaultValue = "")
String secret, @RequestParam(name = "url", required = false, defaultValue = "")
String url, Model model) {
LtiSigner signer = new LtiOauthSigner();
Map<String, String> parameters = new HashMap<>();
parameters.put("lti_message_type","basic-lti-launch-request");
parameters.put("lti_version", "LTI-1p0");
parameters.put("resource_link_id", ((UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
try {
Map<String, String> signedParameters = signer.signParameters(parameters, key, secret, url, "POST");
String link = url + "?" + "oauth_nonce=" + signedParameters.get("oauth_nonce") + "&oauth_signature=" + signedParameters.get("oauth_signature") + "&oauth_consumer_key=" + signedParameters.get("oauth_consumer_key") + "&oauth_signature_method=" + signedParameters.get("oauth_signature_method") + "&oauth_timestamp=" + signedParameters.get("oauth_timestamp") + "&oauth_version=" + signedParameters.get("oauth_version") + "&lti_message_type=" + signedParameters.get("lti_message_type") + "&lti_version=" + signedParameters.get("lti_version") + "&resource_link_id=" + signedParameters.get("resource_link_id");
model.addAttribute("parameters", signedParameters);
model.addAttribute("launchUrl", link);
} catch (LtiSigningException e) {
e.printStackTrace();
}
//model.addAttribute("message", name);
return "start"; //view
}
@GetMapping("") @GetMapping("")
public String start(){ public String start(Model model){
List<Course> courses = courseRepository.findAll();
List<Course> coursesToReturn = new ArrayList<>();
for (Course course : courses) {
LtiSigner signer = new LtiOauthSigner();
Map<String, String> parameters = new HashMap<>();
parameters.put("lti_message_type", "basic-lti-launch-request");
parameters.put("lti_version", "LTI-1p0");
parameters.put("resource_link_id", ((UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
try {
Map<String, String> signedParameters = signer.signParameters(parameters, course.getCourse_key(), course.getSecret(), course.getUrl(), "POST");
String link = course.getUrl() + "?" + "oauth_nonce=" + signedParameters.get("oauth_nonce") + "&oauth_signature=" + signedParameters.get("oauth_signature") + "&oauth_consumer_key=" + signedParameters.get("oauth_consumer_key") + "&oauth_signature_method=" + signedParameters.get("oauth_signature_method") + "&oauth_timestamp=" + signedParameters.get("oauth_timestamp") + "&oauth_version=" + signedParameters.get("oauth_version") + "&lti_message_type=" + signedParameters.get("lti_message_type") + "&lti_version=" + signedParameters.get("lti_version") + "&resource_link_id=" + signedParameters.get("resource_link_id");
course.setSecret(null);
course.setCourse_key(null);
course.setUrl(link);
coursesToReturn.add(course);
} catch (LtiSigningException e) {
e.printStackTrace();
}
}
model.addAttribute("courses", coursesToReturn);
return "start"; //view return "start"; //view
} }
} }

View File

@ -2,6 +2,7 @@ package com.wmi.lti.model.course;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -11,6 +12,7 @@ import javax.persistence.Id;
@Entity @Entity
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor
public class Course { public class Course {
@Id @Id

View File

@ -1,11 +1,13 @@
package com.wmi.lti.model.user; package com.wmi.lti.model.user;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@NoArgsConstructor
public class User { public class User {
@Id @Id

View File

@ -9,9 +9,12 @@
<link rel="stylesheet" th:href="@{/css/main.css}"/> <link rel="stylesheet" th:href="@{/css/main.css}"/>
</head> </head>
<body> <body>
<form th:action="${launchUrl}" method="POST"> <tr th:each="course : ${courses}">
<input type="submit">Launch Tool</input> <form th:action="${course.url}" method="POST">
</form> <input type="submit">Launch Tool</input>
</form>
</tr>
<br><br><br> <br><br><br>
<form th:action="@{/courses/addCourse}" method="GET"> <form th:action="@{/courses/addCourse}" method="GET">
<div><input type="submit" value="Add new content"/></div> <div><input type="submit" value="Add new content"/></div>