-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
314 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/ai/server/controller/AndroidAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ai.server.controller; | ||
|
||
import hibernate.Utente; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Component; | ||
|
||
import dati.Dati; | ||
|
||
@Component | ||
public class AndroidAuthenticationProvider implements AuthenticationProvider { | ||
|
||
@Autowired | ||
private Dati dati; | ||
|
||
public void setDati(Dati dati){ | ||
this.dati = dati; | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
String username = authentication.getName(); | ||
String password = authentication.getCredentials().toString(); | ||
|
||
System.out.println(username); | ||
System.out.println(password); | ||
|
||
Utente utente = dati.getUtenti().get(username); | ||
if(utente != null) { | ||
if(!utente.getConfermato() || !utente.getPassword().equals(password)) | ||
utente = null; | ||
} else { | ||
System.out.println("User " + username + " not found"); | ||
return null; | ||
} | ||
|
||
final List<GrantedAuthority> grantedAuths = new ArrayList<>(); | ||
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); | ||
final UserDetails principal = new User(username, password, grantedAuths); | ||
final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths); | ||
return auth; | ||
|
||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return authentication.equals(UsernamePasswordAuthenticationToken.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,26 @@ | |
import hibernate.Utente; | ||
|
||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.validation.Valid; | ||
|
||
|
||
|
||
|
||
|
||
|
||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.mobile.device.Device; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
@@ -42,11 +51,7 @@ public void setDati(Dati dati){ | |
} | ||
|
||
@RequestMapping(value="/register", method = RequestMethod.GET) | ||
public String showForm(Model model, HttpServletRequest httpServletRequest){ | ||
if(httpServletRequest.getParameter("android-device")!=null){ | ||
System.out.println("eccoci"); | ||
return "eccoci";} | ||
|
||
public String showForm(Model model){ | ||
Registration registration = new Registration(); | ||
model.addAttribute("registration", registration); | ||
|
||
|
@@ -67,7 +72,7 @@ public void processRegistration(@Valid Registration registration, BindingResult | |
return; | ||
} | ||
try{ | ||
dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(),new Date(),numerocasuale); | ||
dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(), new Date(), numerocasuale); | ||
}catch(Exception e){ | ||
System.out.println(e.getMessage()); | ||
System.out.println(e.getStackTrace()); | ||
|
@@ -102,4 +107,53 @@ else if(utente.getConfermato() == true) | |
|
||
} | ||
|
||
|
||
@RequestMapping(value="/android/register", method = RequestMethod.POST) | ||
@ResponseBody | ||
public JSONArray processAndroidRegistration(@Valid Registration registration, BindingResult result, HttpServletRequest request){ | ||
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); | ||
JSONArray response = new JSONArray(); | ||
JSONObject jsonObj = new JSONObject(); | ||
|
||
System.out.println("calling android register"); | ||
|
||
registrationValidation.validate(registration, result); | ||
String numerocasuale = java.util.UUID.randomUUID().toString(); | ||
Map<String, String> errorsMap = new HashMap<String, String>(); | ||
|
||
if(result.hasErrors()){ | ||
|
||
jsonObj.put("status", false); | ||
|
||
for(FieldError fieldError : result.getFieldErrors()) | ||
errorsMap.put(fieldError.getField(), fieldError.getDefaultMessage()); | ||
jsonObj.put("errors", errorsMap); | ||
response.add(jsonObj); | ||
return response; | ||
|
||
} | ||
try{ | ||
dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(), new Date(), numerocasuale); | ||
}catch(Exception e){ | ||
System.out.println(e.getMessage()); | ||
System.out.println(e.getStackTrace()); | ||
jsonObj.put("status", false); | ||
errorsMap.put("database", e.getMessage()); | ||
jsonObj.put("errors", errorsMap); | ||
response.add(jsonObj); | ||
return response; | ||
} | ||
|
||
Mail mail = (Mail) context.getBean("mail"); | ||
|
||
String [] temp = request.getRequestURL().toString().split("/"); | ||
String url = temp[0]+"//"+temp[1]+temp[2]+"/"+temp[3]+"/"; | ||
mail.sendMail("[email protected]", registration.getEmail(), "Registration Confirmation", "Click the link above to confirm your registration\n\n\n"+"<a href='"+url+"confirmregistration?numeroCasuale="+numerocasuale+"&email="+registration.getEmail()+"' />"); | ||
jsonObj.put("status", true); | ||
response.add(jsonObj); | ||
return response; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.