- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我尝试基于 Spring Boot (1.2.7.RELEASE) 和 Vaadin (7.6.3) 构建应用程序。我的问题是我无法将 Spring Security 与 Vaadin 集成。我想要一个自定义的 Vaadin 构建的 LoginScreen 和 Spring Security 控件。我的项目设置如下:
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")).accessDeniedPage("/accessDenied")
.and().authorizeRequests()
.antMatchers("/VAADIN/**", "/PUSH/**", "/UIDL/**", "/login", "/login/**", "/error/**", "/accessDenied/**", "/vaadinServlet/**").permitAll()
.antMatchers("/authorized", "/**").fullyAuthenticated();
}
}
这是我的 Vaadin 登录界面
@SpringUI(path = "/login")
@Title("LoginPage")
@Theme("valo")
public class LoginUI extends UI {
TextField user;
PasswordField password;
Button loginButton = new Button("Login", this::loginButtonClick);
private static final String username = "username";
private static final String passwordValue = "test123";
@Override
protected void init(VaadinRequest request) {
setSizeFull();
user = new TextField("User:");
user.setWidth("300px");
user.setRequired(true);
user.setInputPrompt("Your username");
password = new PasswordField("Password:");
password.setWidth("300px");
password.setRequired(true);
password.setValue("");
password.setNullRepresentation("");
VerticalLayout fields = new VerticalLayout(user, password, loginButton);
fields.setCaption("Please login to access the application");
fields.setSpacing(true);
fields.setMargin(new MarginInfo(true, true, true, false));
fields.setSizeUndefined();
VerticalLayout uiLayout = new VerticalLayout(fields);
uiLayout.setSizeFull();
uiLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
setStyleName(Reindeer.LAYOUT_BLUE);
setFocusedComponent(user);
setContent(uiLayout);
}
public void loginButtonClick(Button.ClickEvent e) {
//authorize/authenticate user
//tell spring that my user is authenticated and dispatch to my mainUI
}
}
当我启动我的应用程序时,spring 将我重定向到我的登录 UI,这很好。
但我不知道如何根据 spring 安全机制对用户进行身份验证并发送到我的 mainUI。
我也面临着 csrf token 的问题,如果我不禁用 csrf,我将得到 crfs token is null 异常。我找到了很多处理这些问题的示例,但是 Vaadin 没有提供解决方案。
感谢您的帮助。
最佳答案
经过一周的努力和研究,我终于可以正常工作了。非常累,因为网上有很多信息和解决方案,其中大部分使用基于 xml 的配置或基于 JSP 表单的登录,直到现在我找不到没有 xml 配置文件的解决方案,使用 Vaadin 框架创建一个自定义登录页面。
我不能保证这是最佳实践或最简单的解决方案。此外,我没有评估它的每个部分,登录机制就我所见有效,但可能存在一些我尚未发现的问题。
也许它会帮助面临同样问题的人,所以我会在这里发布我的答案。
首先是我的安全配置:
@Resource(name = "authService")
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")).accessDeniedPage("/accessDenied")
.and().authorizeRequests()
.antMatchers("/VAADIN/**", "/PUSH/**", "/UIDL/**", "/login", "/login/**", "/error/**", "/accessDenied/**", "/vaadinServlet/**").permitAll()
.antMatchers("/authorized", "/**").fullyAuthenticated();
}
@Bean
public DaoAuthenticationProvider createDaoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
您必须禁用 crsf,但这没问题,因为 vaadin 有自己的 crsf 保护。
此外,您需要允许一些 URI,以便 vaadin 可以访问其资源:/VAADIN/**
是绝对必要的,我也建议允许 /vaadinServlet/**
、/PUSH/**
和 /HEARTBEAT/**
,但这取决于您使用的 Vaadin 的哪些部分。
第二个UserDetailsService
:
@Service("authService")
public class AuthService implements UserDetailsService {
@Autowired
CustomUserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
CustomUser user = userRepository.findCustomUserByUserName(userName);
return user;
}
}
DaoAuthenticationProvider
使用 UserDetails
的 loadUserByUserName
方法获取实现了 UserDetails
的类的对象界面。请注意,UserDetailsInterface
中描述的每个属性都不能为 null,否则您稍后会收到 DaoAuthenticationProvider
抛出的 NullPointerException
。
我创建了一个实现 UserDetails 接口(interface)的 JPA 实体:
@Entity
public class CustomUser implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@ManyToMany(fetch = FetchType.EAGER)
Collection<Authorities> authorities;
String password;
String userName;
Boolean accountNonExpired;
Boolean accountNonLocked;
Boolean credentialsNonExpired;
Boolean enabled;
@Autowired
@Transient
BCryptPasswordEncoder passwordEncoder;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return userName;
}
@Override
public boolean isAccountNonExpired() {
return accountNonExpired;
}
@Override
public boolean isAccountNonLocked() {
return accountNonLocked;
}
@Override
public boolean isCredentialsNonExpired() {
return credentialsNonExpired;
}
@Override
public boolean isEnabled() {
return enabled;
}
public void setId(Long id) {
this.id = id;
}
public void setAuthorities(Collection<Authorities> authorities) {
this.authorities = authorities;
}
public void setPassword(String password) {
this.password = passwordEncoder.encode(password);
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setAccountNonExpired(Boolean accountNonExpired) {
this.accountNonExpired = accountNonExpired;
}
public void setAccountNonLocked(Boolean accountNonLocked) {
this.accountNonLocked = accountNonLocked;
}
public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
this.credentialsNonExpired = credentialsNonExpired;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
}
加上权威实体:
@Entity
public class Authorities implements GrantedAuthority {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
String authority;
@Override
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
显然,在身份验证生效之前,您必须先将一些用户数据存储在数据库中。
在 Vaadin 中,我无法通过使用具有不同 View 的一个 UI 来实现它,所以我最终使用了两个 UI,一个用于登录,另一个用于主应用程序。
在 Vaadin 中,我可以在类注释中设置 URI 路径:
@SpringUI(path = "/login")
@Title("LoginPage")
@Theme("valo")
public class LoginUI extends UI {
//...
}
使用此配置,我的登录屏幕在 localhost:port/login
可用我的主要应用程序位于 localhost:port/main
。
我在 loginUI 的 button.click 方法中以编程方式登录用户:
Authentication auth = new UsernamePasswordAuthenticationToken(userName.getValue(),password.getValue());
Authentication authenticated = daoAuthenticationProvider.authenticate(auth);
SecurityContextHolder.getContext().setAuthentication(authenticated);
//redirect to main application
getPage().setLocation("/main");
希望对大家有所帮助。
关于java - 使用 Vaadin 登录的 Spring Boot 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912404/
我正在尝试构建一个托管在服务器上的 Blazor 应用程序,起点位于一个 razor 页面内。 类似的东西: 我的问题是: 如果 Razor 页面具有授权属性,所有 blazor 代码都不是通过身份
对tomcat中管道和阀门机制不懂的小伙伴,参考本篇文章 领域 目前可知结构,如图所示,下面继续分析 GenericPrincipal类 LoginConfig类 Authenticator接口 在T
我刚刚开始学习 React 中的授权和身份验证,我在使用 JWT 完成我的第一个简单登录系统后编写了这篇文章,因为大多数人都知道您在浏览器中存储了一个 token ,然后将其与保存的进行比较现在,当验
我正在为grails项目寻找安全插件。 Spring安全核心1.2.7.3看起来很棒,但是似乎已经有将近一年没有开发了。有谁知道是这样吗? 还有其他好的插件吗? 我也正在使用mongodb,想知道sp
我有一个 WCF 服务,它使用具有消息安全性和用户名身份验证的 NetTcpBinding。在此之前,我使用 WsHttpBinding 但我切换到 NetTcp,因为我可以使用回调。 我的服务配置如
我正在考虑使用 cakePHP 构建一个 Web 应用程序。我的问题是我必须自己编写多少安全内容来防止(SQL 注入(inject)等)? cakePHP 自己处理什么安全问题,我必须编写什么代码?
任何人都有关于为安全环境强化/配置 TFS 的信息? 最佳答案 TFS 使用 Windows 身份验证,因此它与您的网络一样安全。我建议您还查看有关加强网络安全的资源。 Team Foundation
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我一直想知道 codeigniter 设置有多安全。因为数据库密码等信息存储在主应用程序文件夹的配置文件中,黑客可以检索到这些信息吗?我知道您可以将应用程序文件夹移动到远离 Web 根目录的位置,但如
在客户端使用 PouchDB 访问远程服务器时要遵循的最佳安全实践是什么? https://pouchdb.com/getting-started.html上的例子使用代码与远程服务器同步: var
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在开发一个网络应用程序,用户可以在其中上传文件并执行它们。我的意思是,他们可以上传 html 文件,然后通过单击,他们可以在我的 Web 应用程序内的 iframe 中执行该文件并查看渲染的 ht
试图澄清 -AsPlainText ConvertTo-SecureString 中的参数小命令: -AsPlainText Specifies a plain text string to conv
我正在玩 coreos 和 digitalocean,我想开始允许我的容器之间进行内部通信。 我已经为所有主机设置了私有(private)网络,现在我想确保某些容器只打开到 localhost 和内部
我们有两台机器: 服务器 客户 服务器正在运行 Clojure + Ring + ...标准 ClojureScript webstack。 客户端 = 某些运行 Chorme/Firefox/Saf
让我们有一个函数 foo 和一个类 Bar: fun foo(key: String): String? { // returns string or null } class Bar(x: St
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
确保只有正确的用户才能在 Sharepoint 2007 中看到 Web 部件的最佳做法是什么? 有人向我建议了安全组和受众。 最佳答案 这取决于您是在谈论 Web 部件的呈现还是从 Web 部件库添
我试图说服一个团队,使用 jQuery JSONP 调用与不受信任的第三方可能是不安全的。我正在使用标准 jQuery 代码: $.ajax({ url:unsecureserver+"?js
我有以下ajax调用,它检查用户是否是付费成员(member),如果是,则相应地运行某些功能。这可行,但我担心安全性。如果有人在控制台中更改此 ajax 代码,强制 #button 成功运行功能而无需
我是一名优秀的程序员,十分优秀!