not working

This commit is contained in:
unknown 2020-05-02 12:34:37 +02:00
parent 096386e7aa
commit 04c6247522
16 changed files with 304 additions and 31 deletions

View File

@ -8,9 +8,11 @@ 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("/home").setViewName("home"); registry.addViewController("/addCourse").setViewName("addCourse");
registry.addViewController("/").setViewName("home"); registry.addViewController("/").setViewName("start");
registry.addViewController("/login").setViewName("login"); registry.addViewController("/login").setViewName("login");
registry.addViewController("/start").setViewName("start");
registry.addViewController("/register").setViewName("register");
} }
} }

View File

@ -1,23 +1,43 @@
package com.wmi.lti.config; package com.wmi.lti.config;
import com.wmi.lti.model.user.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; 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.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@ComponentScan
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configAuthentication(final AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.authorizeRequests() .authorizeRequests()
.antMatchers("/", "/home").permitAll() .antMatchers( "/courses/**").authenticated()
.antMatchers("/", "/register").permitAll()
.antMatchers("/", "/start").authenticated() .antMatchers("/", "/start").authenticated()
//.anyRequest().authenticated() //.anyRequest().authenticated()
.and() .and()
@ -34,13 +54,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Bean
@Override @Override
public UserDetailsService userDetailsService() { public UserDetailsService userDetailsService() {
UserDetails user = /* UserDetails user =
User.withDefaultPasswordEncoder() User.withDefaultPasswordEncoder()
.username("adrianw") .username("adrianw")
.password("eduserwis1245") .password("eduserwis1245")
.roles("USER") .roles("USER")
.build(); .build();
*/
return new InMemoryUserDetailsManager(user); return new MyUserDetailsService() {
};
} }
} }

View File

@ -0,0 +1,52 @@
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 org.imsglobal.lti.launch.LtiOauthSigner;
import org.imsglobal.lti.launch.LtiSigner;
import org.imsglobal.lti.launch.LtiSigningException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap;
import java.util.Map;
@Controller
public class CourseController {
@Autowired
private CourseRepository courseRepository;
@GetMapping("/courses")
public String listCourses() {
//model.addAttribute("message", name);
return "start"; //view
}
@GetMapping("/addCourse")
public String addCourseView() {
return "addCourse"; //view
}
@PostMapping("")
public String addCourse(
@RequestParam(name = "key", required = false, defaultValue = "")
String key, @RequestParam(name = "secret", required = false, defaultValue = "")
String secret, @RequestParam(name = "url", required = false, defaultValue = "")
String url, @RequestParam(name = "name", required = false, defaultValue = "")
String name, Model model) {
courseRepository.save(new Course(null, name, url, key, secret));
return "start"; //view
}
}

View File

@ -1,20 +0,0 @@
package com.wmi.lti.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
@GetMapping("/home")
public String home(
@RequestParam(name = "name", required = false, defaultValue = "")
String name, Model model) {
//model.addAttribute("message", name);
return "home"; //view
}
}

View File

@ -1,17 +1,15 @@
package com.wmi.lti.controllers; package com.wmi.lti.controllers;
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.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller; 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.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -29,7 +27,7 @@ public class StartController {
Map<String, String> parameters = new HashMap<>(); Map<String, String> parameters = new HashMap<>();
parameters.put("lti_message_type","basic-lti-launch-request"); parameters.put("lti_message_type","basic-lti-launch-request");
parameters.put("lti_version", "LTI-1p0"); parameters.put("lti_version", "LTI-1p0");
parameters.put("resource_link_id", ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); parameters.put("resource_link_id", ((UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
try { try {
Map<String, String> signedParameters = signer.signParameters(parameters, key, secret, url, "POST"); 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"); 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");

View File

@ -0,0 +1,38 @@
package com.wmi.lti.controllers;
import com.wmi.lti.model.user.User;
import com.wmi.lti.model.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@PostMapping("")
public String create(@RequestParam String username, @RequestParam String password, Model model) {
User user = new User();
user.setUsername(username);
user.setPassword(passwordEncoder.encode(password));
userRepository.save(user);
return "login";
}
@GetMapping("/register")
public String getRegisterView() {
return "register";
}
}

View File

@ -0,0 +1,38 @@
package com.wmi.lti.model.course;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@AllArgsConstructor
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private Long id;
@Getter
@Setter
private String name;
@Getter
@Setter
private String url;
@Getter
@Setter
private String course_key;
@Getter
@Setter
private String secret;
}

View File

@ -0,0 +1,9 @@
package com.wmi.lti.model.course;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CourseRepository extends JpaRepository<Course, Long> {
}

View File

@ -0,0 +1,24 @@
package com.wmi.lti.model.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
return new UserPrincipal(user);
}
}

View File

@ -0,0 +1,26 @@
package com.wmi.lti.model.user;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private Long id;
@Getter
@Setter
@Column(nullable = false, unique = true)
private String username;
@Getter
@Setter
private String password;
}

View File

@ -0,0 +1,49 @@
package com.wmi.lti.model.user;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class UserPrincipal implements UserDetails {
private User user;
public UserPrincipal(User user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}

View File

@ -0,0 +1,9 @@
package com.wmi.lti.model.user;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}

View File

@ -10,10 +10,11 @@
</head> </head>
<body> <body>
This is tool consumer for LTI<br> This is tool consumer for LTI<br>
<form th:action="@{/start}" method="post"> <form th:action="@{/courses}" method="post">
<div><label> URL: <input type="text" name="url"/></label></div> <div><label> URL: <input type="text" name="url"/></label></div>
<div><label> Key: <input type="text" name="key"/></label></div> <div><label> Key: <input type="text" name="key"/></label></div>
<div><label> Secret: <input type="text" name="secret"/></label></div> <div><label> Secret: <input type="text" name="secret"/></label></div>
<div><label> Course name: <input type="text" name="name"/></label></div>
<div><input type="submit" value="Submit"/></div> <div><input type="submit" value="Submit"/></div>
</form> </form>
</body> </body>

View File

@ -16,5 +16,9 @@
<div><label> Password: <input type="password" name="password"/> </label></div> <div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div> <div><input type="submit" value="Sign In"/></div>
</form> </form>
<form th:action="@{/users/register}" method="get">
<div><input type="submit" value="Register"/></div>
</form>
</body> </body>
</html> </html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>LTI start</title>
<link rel="stylesheet" th:href="@{/css/main.css}"/>
</head>
<body>
<form th:action="@{/users}" method="post">
<div><label> User Name : <input type="text" name="username"/></label></div>
<div><label> Password: <input type="password" name="password"/></label></div>
<div><input type="submit" value="Register"/></div>
</form>
</body>
</html>

View File

@ -12,5 +12,9 @@
<form th:action="${launchUrl}" method="POST"> <form th:action="${launchUrl}" method="POST">
<input type="submit">Launch Tool</input> <input type="submit">Launch Tool</input>
</form> </form>
<br><br><br>
<form th:action="@{/courses/addCourse}" method="GET">
<div><input type="submit" value="Add new content"/></div>
</form>
</body> </body>
</html> </html>